• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Load the same libraries in every controller?

#1
[eluser]Lpeek[/eluser]
Hey, I'm just getting started with CI, and just wondering whether I can do any kind of global load for controllers. I know I could go and edit the main system controller but I really dont want to do that.

The reason being that on every page of my site, I will be loading the header, which needs the session and cart libraries. There is no page on the site that wont have a cart summary.

Would the normal way be to just load the same things and same views etc in every controller? or is there a way to reuse this kind of functionality? Maybe I could create a header library/helper? and do everything relating to the header/cart in that before loading that within every controller?

Thanks!

#2
[eluser]alexwenzel[/eluser]
checkout the application/autoload.php file.

thats exactly what you ask for!

#3
[eluser]solid9[/eluser]
Learn MY_Controller...
Or you can auto load the library in the config (autoload.php)


#4
[eluser]CroNiX[/eluser]
What you are asking about is called Autoloading

As far as your view question, I create a template library (can be done with a helper too). That library automatically creates the header, menu system, footer, sidebars, etc, and populates it with the current logged in user info, shopping cart etc, automatically. All I do is pass it the current content that that controller is producing (the main view) and send it to the template which automatically inserts it where its supposed to go. Then you don't have to include the header/footer/etc in each of your controllers. Just the main view and have the template library/helper do the rest to keep your code a LOT cleaner and smaller.

#5
[eluser]jojo777[/eluser]
[quote author="CroNiX" date="1351181468"]What you are asking about is called Autoloading

As far as your view question, I create a template library (can be done with a helper too). That library automatically creates the header, menu system, footer, sidebars, etc, and populates it with the current logged in user info, shopping cart etc, automatically. All I do is pass it the current content that that controller is producing (the main view) and send it to the template which automatically inserts it where its supposed to go. Then you don't have to include the header/footer/etc in each of your controllers. Just the main view and have the template library/helper do the rest to keep your code a LOT cleaner and smaller.[/quote]

That sounds cool, I've a view called template.php that loads the header-content-footer where content is the main view of the section, but your system sounds more advanced, Hav you got some tutorial to see that kind of templates?

Thanks.

#6
[eluser]CroNiX[/eluser]
Here's a very basic/simple helper I posted a few days ago to show the concept.

Normally I create a library with a lot more functionality and different page "types" to serve.

#7
[eluser]Lpeek[/eluser]
thanks CroNiX! Your method sounds great, and to others suggesting autoload.php thats also brilliant thanks! I had been putting lots of loading in the construct of every controller and hadn't noticed the autoload ability!

Will try all of your tips and I expect I'll get what I'm after!

Thanks!

#8
[eluser]jojo777[/eluser]
[quote author="CroNiX" date="1351185304"]Here's a very basic/simple helper I posted a few days ago to show the concept.

Normally I create a library with a lot more functionality and different page "types" to serve.[/quote]

Cool! thanks!


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2021 MyBB Group.