开发者

Library calls in header view - CodeIgniter

开发者 https://www.devze.com 2023-03-30 03:48 出处:网络
I need to call some library functions in order to correctly display some data in my header view - what is the best/correct way to approach this?

I need to call some library functions in order to correctly display some data in my header view - what is the best/correct way to approach this?

I take it I'm supposed to call this in all my controllers but it seems a bit redundant. Should I take this route? What sort of non 'hacky' method do you suggest?

Edit:

I have a view which outputs the header portion of the page. In this view I have a menu which varies depending whether you're logged in, have any favorites etc. To determine what to display in the menu, I must refer to some libraries, for example, an authentication and favorites library.

I tried calling the library from the view, but it gives me an error.

I suppose I could load开发者_开发百科 the controller and pass the data to the view in every controller but I would have a lot of repetitive code. Is this the way to go?


Yes, manually assigning the same data to a partial view in every controller or method is redundant. You want to avoid this.

Any decent template library should be able to handle this for you. You can google up a solution or write your own.

Here, in mockup code, is what it may resemble:

class Template {

    public $data = array();

    function area($area_name)
    {
        $CI =& get_instance();
        $data = isset($this->data[$area_name]) : $this->data[$area_name] ? NULL;
        $CI->load->view('templates/'.$area_name, $data);
    }

    function set_data($area_name, $data)
    {
        $this->data[$area_name] = $data;
    }
}

Then in the controller, something like this:

$this->template->set_data('header', $my_data);

Then in the view, something like this:

<header><?php echo $this->template->area('header'); ?></header>
<div><?php echo $this->template->area('content'); ?></div>
<footer><?php echo $this->template->area('footer'); ?></footer>

This is over-simplified, and there are a million ways to handle it, but I definitely suggest writing some kind of class to handle your templates rather than just using $this->load->view() for everything, even if it is just a wrapper for loading views.

To avoid manually setting that same data in every controller, use a MY_Controller, set it in the template class __construct(), or call it directly from the view file from whatever the source is (model, library, etc.). Father MVC may shed a tear, but sometimes this is easiest or even makes the most sense.

Quick example of how to use a controller extension:

// File: application/core/MY_Controller.php
class MY_Controller extends CI_Controller {

    public function __construct()
    {
        parent::__construct();
        $data['var_name'] = $this->some_lib->get_data();
        $data['var_name2'] = $this->some_model->get_more_data();
        $this->template->set_data('header', $data);
    }
}

Then, all your controllers would extend MY_Controller rather than CI_Controller and the header data would already be loaded. This is just a simplified glimpse into another topic altogether, much much more is possible with this method.

I tried calling the library from the view, but it gives me an error.

This shouldn't happen if the library is loaded and you are calling it correctly. The syntax is $this->library_name->method().

In any case, I definitely recommend writing or borrowing some kind of Template class.


You don't have to follow the MVC model, but if you want to, the "correct" way is to call the functions in the controller, then pass the data to the view where it's printed out.


You should be able to just autoload the library or else load it in the controller before the view. Then you'll have access to the library methods in your views and can access them the same way you would in your controllers.

0

精彩评论

暂无评论...
验证码 换一张
取 消