views:

85

answers:

2

I'm about to write a admin panel for my CMS written in CodeIgniter. There will be some user information visible at all time - both in the layout's header section and the sidebar. I'm used to do it in a way that I personally hope and think could be done a lot easier, since I'm tired of sending the same parametres to the view over and over again, when it's dynamic data that needs to be displayed on every page anyways (such as unread messages, username, name, status, etc).

I'll need controllers and models, I know that, but do I have to pass, just for an example, the user's username, unread messages etc. every time I need to load a view? Should I do some kind of library for this?

Now my question is: How would I do it when it comes to best practice and for making it easy to maintain in the future?

I hope my question is understandable :)

A: 

You could create a View just to hold the information and get it from a $_SESSION variable in the View itself if you want to keep it all in one place.

Arda Xi
Not sure that I want to use sessions for this. I can see your point, but I would love to see a solution without using sessions though.
rkj
It was an example. Any way you're getting the data will suffice from within the View, it's just another PHP file after all. It doesn't adhere to the most strict definitions of MVC, but it does work.
Arda Xi
Exactly my thought too, that it doesn't adhere, which I would like. Though thanks a lot for your answer mate. :)
rkj
If you want to follow MVC strictly, you'll have to insert the views and send the parameters to it from your controller. You can't get around that. You'll have to "pass, just for an example, the user's username, unread messages etc. every time [you] need to load a view" since that is the MVC way.
Arda Xi
+1  A: 

Personally, I would extend the Controller library (create a MY_Controller by following the guidance at the bottom of Creating Libraries at codeigniter.com).

You would use your model etc as normal. Then you would create a private function in your MY_Controller class to get the relevant "global" data and call

$this->load->vars('everywhere_data', $data_from_relevant_models);

which would make the data available to all views called from that point on as $everywhere_data. Then add a reference to that function in the constructor of MY_Controller, perhaps with a conditional checking for the user to be actually logged in.

If it's complex to collect and get all that data, you might write a library to handle it for you, but the 'controller' part would still be done by MY_Controller: i.e. to get the data and then use load->vars() to publish it to the view.

As a quick and untested example, MY_Controller would start something like as follows:

<?php
class MY_Controller extends Controller
{
    private $logged_in_user;


    function MY_Controller()
    {
        parent::Controller();

        if( $this->_logged_in_userid() > 0 )
        {   
            $this->logged_in_user = $this->_get_user( $this->logged_in_userid() );

            $this->load->vars('logged_in_username', $this->logged_in_user->username );
        } else {
            $this->logged_in_user = false;
        }

    }
...
}

Note that things like _logged_in_userid() would access the session for you (e.g. return $this->session->userdata('logged_in_userid');), and _get_user() would access the relevant models.

Finally, you would have a view that accesses $logged_in_username (or everywhere_data in my first example) which you would call into your headers etc. This leaves your normal controllers uncluttered so that they can focus on delivering their specific functionality, stops you rewriting your code several times AND maintains the MVC ideals.

Kurucu
Very useful and a very interesting way of doing this. Thanks a lot for your help and thoughts :)
rkj
No problem! That's what this site is all about! No doubt you'll answer a question I pose one day soon :)
Kurucu