CodeIgniter Forums

Full Version: Global controller?
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

[eluser]Sein Kraft[/eluser]
I've a header that should show the username and another things. But I don't want to declare in every function $this->load->view('header', $data);.

There is ani way to avoid that?

El Forum

[eluser]jedd[/eluser]
[quote author="Sein Kraft" date="1259913699"]
But I don't want to declare in every function $this->load->view('header', $data);.

There is ani way to avoid that?[/quote]

Yes - but you have to read the [url="/wiki/FAQ"]FAQ[/url].

El Forum

[eluser]Sein Kraft[/eluser]
I've tried that without luck.

This MY_Controller.php
Code:
<?php

class Users extends MY_Controller {

    function Users()
    {
        parent::MY_Controller();
    }
    
    function my_user()
    {
        return "username";
    }
}
?>

Quote:Fatal error: Class 'MY_Controller' not found in D:\Server\htdocs\system\libraries\MY_Controller.php on line 3

El Forum

[eluser]Colin Williams[/eluser]
Your MY_Controller.php must define the MY_Controller class. Then, later classes will extend it.

El Forum

[eluser]Sein Kraft[/eluser]
Thanks, now is working.

But still have the doubt about that because...for example. I need the "global" header but where i put this?

Code:
$this->load->library('parser');

$data = array(
            'username' => $this->username_from_session(),
            'new_messages' => $this->new_messages()
            );

$this->parser->parse('header', $data);

Because I'm putting this on every function and is a bit messy, I want to avoid that.

Should I put that on:
Code:
function Users(){
    
        parent::MY_Controller();
    
}

And then the view for each funcion in the funtion? and whats happend with the footer where I should put it?

El Forum

[eluser]stuffradio[/eluser]
MY_Controller

Code:
<?php

class MY_Controller extends Controller
{

      function MY_Controller()
      {
          parent::Controller();
       $this->parser->parse('header', $data);
      }

}

Yourcontroller.php

Code:
<?php

class Yourcontroller extends MY_Controller
{

     function Yourcontroller()
     {
        parent::MY_Controller();
     }

     function yourfunction()
     {
       // header loaded first
       $this->load->view('myview');
     }
}

El Forum

[eluser]Sein Kraft[/eluser]
Thanks, this was very useful.

BBut another question, what happend with the footer?

I should load it like the custom views in functions?

El Forum

[eluser]stuffradio[/eluser]
Not sure what to do for footers, I personally load the footer underneath the page.

Code:
$this->load->view('myview');
$this->load->view('footer');

El Forum

[eluser]Sein Kraft[/eluser]
Yep, I was thinking that.