Welcome Guest, Not a member yet? Register   Sign In
Controller Constants
#1

[eluser]BradEstey[/eluser]
Every page that I call needs to run the same model first to get the page header/meta tags/section id/page template/etc from the database. Currently I'm putting the same line of code at the top of every Controller Function to call that Model (Pageload->getPage()) and return the page info for that particular page. There has got to be an automated way to do this, does anyone know where?

Even if I have to call it once per Controller somehow, that would still be better than calling it for every function.

Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();    
        session_start();
    }

    function index()
    {
        $data['pageinfo'] = $this->Pageload->getPage();
        
        $data['load_js'] = array('jquery', 'reliant');
        $this->load->view($data['pageinfo']->template, $data);
    }
    
    function contact()
    {
        $data['pageinfo'] = $this->Pageload->getPage();
        
        $data['contact'] = $this->Contact->getAddress();
        $data['load_js'] = array('jquery', 'reliant', 'googlemaps');
        $this->load->view($data['pageinfo']->template, $data);
    }    
}
#2

[eluser]danmontgomery[/eluser]
You have a couple of options.

- Extend the controller class, call the code in the constructor of that base class so that it is automatically called whenever a controller extends the base class.
- Put it in the constructor of each controller that needs to call it.

Either way, the variable would need to be assigned to a class member, not a local variable.

Code:
class Home extends Controller {

    function Home()
    {
        parent::Controller();
        $this->pageinfo = $this->Pageload->getPage();
        session_start();
    }

    function index()
    {
        $data['contact'] = $this->Contact->getAddress();
        $data['load_js'] = array('jquery', 'reliant', 'googlemaps');
        $this->load->view($this->pageinfo->template, $data);
    }
#3

[eluser]BradEstey[/eluser]
Worked like a charm! I knew I would probably have to call it in the constructor but I wasn't sure how to pass those variables to all of the functions. I guess I overlooked the obvious! Smile

Thanks noctrum!




Theme © iAndrew 2016 - Forum software by © MyBB