Welcome Guest, Not a member yet? Register   Sign In
Loading the same data for usage in sitewide views
#1

[eluser]Moleman[/eluser]
Ok, my site consists of several controllers and all the controllers is using the same the same top and bottom view files.

But I want to add data from the database in those two sitewide views. (Like userinfo, latest posts etc)

How can I do this without having to load and set up the same models in every action I add?

I guess I could set the view data in the controller constructors, but that's still a bit of a pain in the butt.

How are things like these usually done in CI?

Thanks in advance!
#2

[eluser]jedd[/eluser]
[quote author="Moleman" date="1236307837"]
I guess I could set the view data in the controller constructors, but that's still a bit of a pain in the butt.

How are things like these usually done in CI?[/quote]

I asked a similar question recently.

The suggested method was to extend the default Controller class, making my own MY_Controller class, and then using that as the default class that Controllers would extend.

You do this in system/application/libraries/MY_Controller:

Code:
class MY_Controller  extends  Controller  {
    function MY_Controller ()  {
        parent::Controller();

        $this->data['main_menu_data'] = $this->_generate_menu_data();
        }

_generate_menu_data() is a private method within this class that goes and generates the menu that is in pretty much every view, in the 'menu div'.

All my controllers now start with:

Code:
class Organism extends MY_Controller {
    function Organism()  {
        parent::MY_Controller();

        }

Have a read of [url="http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html"]http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html[/url] for more info.
#3

[eluser]Moleman[/eluser]
Excellent! Thanks!

I was thinking about extending the controller, but I never thought about adding it in the library folder.

Thanks!
#4

[eluser]Colin Williams[/eluser]
Or, to lessen the complexity, just drop system/libraries/Controller.php into application/libraries/Controller.php and start modifying it to suit your needs.
#5

[eluser]Moleman[/eluser]
Thanks,
One more thing though, do I have to include the new library manually? (eg. include 'My_Controller')
Because it doesn't seem to work with the CI autoload feature.
#6

[eluser]Colin Williams[/eluser]
If you use the correct prefix (e.g. "MY_") then it will load automatically. Have a read of http://ellislab.com/codeigniter/user-gui...aries.html for more info.
#7

[eluser]jedd[/eluser]
Colin - doesn't that approach get messy when you upgrade to a new version of CI?

[quote author="Moleman" date="1236311482"]
One more thing though, do I have to include the new library manually? (eg. include 'My_Controller')
Because it doesn't seem to work with the CI autoload feature.[/quote]

Hmm .. go and re-read the code snippets I posted above.

You have your MY_Controller - and that becomes the new class that you extend in all your application/controller/xyz.php Controller files.

You don't have to auto-load anything here. Your config/config.php includes a line:
Code:
$config['subclass_prefix'] = 'MY_';
that tells CI to look out for any classes in the application/libraries directory that starts with the string "MY_" (Well, effectively this is what it does).

Does that make more sense?
#8

[eluser]Moleman[/eluser]
Oh yeah, I get it now.
It was my bad though, I actually called my controller Dp_Controller, never knew about the subclass prefix setting.

Everything is working fine now Smile
#9

[eluser]Colin Williams[/eluser]
Quote:Colin - doesn’t that approach get messy when you upgrade to a new version of CI?

Good point.
#10

[eluser]rogierb[/eluser]
Maybe you could use a hook? Like the post_controller_constructor or the pre_controller? And with that create views you can use in your main template. This should theoretically be possible but I havn't tried it.




Theme © iAndrew 2016 - Forum software by © MyBB