Welcome Guest, Not a member yet? Register   Sign In
Accessing a library from within a view
#1

[eluser]lkagan[/eluser]
So I have this library that I try to access from within a view. The library appears to be loading fine but apparently I'm accessing it incorrectly.

Note that the view is a 'partial' in that it is being called from a typical view with
Code:
<? $this->load->view('_header'); ?>

It appears to be loading fine because no errors are generated until I try to access the object. Here's is the code that is causing the error:

Code:
$this->load->library('remotecredentials');
$credentials = $this->remotecredentials->getAllCredentials();

The notice I get is:
Quote:Undefined property: CI_Loader::$remotecredentials

Does anyone see what the issue is? Thanks in advance.
#2

[eluser]xwero[/eluser]
It's better that you load your library in the controller and add the data to the view using the second argument of the view method.

Did you put the library in the application/libraries directory?
#3

[eluser]lkagan[/eluser]
I agree that to stay true to the MVC design pattern, I should load the library in the controller. But since this bit of information needs to be available to all views and little logic exists, I'll save myself the extra work.

Yes, the library is in the application/libraries directory.
#4

[eluser]xwero[/eluser]
By all views you mean, all views that make up the page or all pages of the website?
#5

[eluser]lkagan[/eluser]
I should have written 'all pages', not all views. My apologies. I know I can auto load the library but I would rather be more explicit so other coders can more easily understand the code.
#6

[eluser]xwero[/eluser]
what you can do is extending the CI controller and use that extended controller instead of the CI controller or you can add a (pre-controller) hook.

Extending the controller is very explicit but if that's the only thing you want to change for every controller i think you are better of calling the method using a hook.
#7

[eluser]lkagan[/eluser]
Okay, so you're coming up with other ideas. Are you specifically saying that a library can't be loaded and used directly in a view? If that's the case, do you know why wouldn't any notices be given when loading the library with $this->load->library() and error_reporting set to E_ALL.

If it's not possible, I think I'll just use the auto loader.

Thanks for following this thread through.
#8

[eluser]xwero[/eluser]
I'm not sure if a library can be loaded in a view but is not a good practice. And if the only reason is readability of your code encouraging bad practice is not right way to build your app/website.

I know you can't load a model in a view so why would loading a library be different.
#9

[eluser]esra[/eluser]
Are you declaring $remotecredentials as an object in scope throughout the class?


Code:
class RemoteCredentials {

    var $remotecredentials = '';

    RemoteCredentials()
    {
        // .. construction code
    }
}

I would agree that library loading should be handled by the controller, but not necessarily using a hook. You could create an base controller and extend all other controllers from that controller.

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

class AppController extends Controller {

    function AppController()
    {
        parent::Controller();    
        $this->load->library('view');
        $this->load->library('validation');
        $this->load->library('remotecredentials');
        $this->load->helper( array('path', 'html', 'file') );
    }
    
    function index()
    {
    }
}

Below, the controller called Users would inheirit the libraries loaded in the constructor code from its parent controller making it available to all of its views.

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

require(APPPATH.'libraries/appcontroller'.EXT);

class Users extends AppController {

    function Users()
    {
        parent::AppController();    
        $this->load->model('users_model', '', TRUE);
        $this->lang->load('users', 'en_us');
    }
    
    function index()
    {
        $module = $this->lang->line('users_module');
        $title = $this->lang->line('users_title');
        
        $this->view->set('title', $title);
        $this->view->set('module', $module);
        $this->view->part('west', 'west');
        $this->view->part('north', 'north');
        $this->view->part('autotabs', 'autotabs');
        $this->view->part('content', $module.'_view');
        $this->view->part('props_panel', 'props_panel');
        $this->view->part('south', 'south');
        $this->view->load('template');
    }
}
?>
#10

[eluser]lkagan[/eluser]
@xwero: Thanks for your reply. One thing I've learned in my ten years of writing web applications is that nothing is written in stone. Best practices are great and I encourage anyone and everyone to follow them as I suggest here: http://www.superiocity.com/blog/view/PHP...-Practices

However, there are times when best practices should be viewed for what they are, guidelines. Without knowing the full scope (or any scope for that matter) of the application or the support resources available for that application, suggesting that the method I choose in building an application is 'not the right way' is presumptuous.

I do appreciate your engineering fundamentalism and with your 135 posts, I appreciate your willingness to help the community even more.

@esra: I thought the issue might lie in something as silly as a naming conflict as you suggest, but after a quick review, there is no conflict. Controller inheritance is a good idea and was already suggested by xwero.

Once again, thanks for the suggestions.




Theme © iAndrew 2016 - Forum software by © MyBB