Show Menu on every Page... |
[eluser]Unknown[/eluser]
Hi Everybody... I'm pretty newbie using CodeIgniter, and there's something i wanna do but i don't know the better way to do it. I've made this controller, which shows a Menu bar. Code: <?php My idea is to show this menu on every single page from my website. Suposing that i'm in another controller, what's the better way to invoke this menu, besides, is this a good way to make a menu? Thanks in advance... Greetings.
[eluser]jedd[/eluser]
Hi jasv, and welcome to the CI forums. The magic you're looking for is [url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]extending the core Controller - MY_Controller[/url]. This will help you reduce your repetitive coding problem. Here's a [url="http://ellislab.com/forums/viewthread/110969/"]good thread on the subject[/url]. EDIT - Make a menu .. uhm, depends - is this something dynamic, or something pretty static - are you pulling it from a database, will it be configurable within your application or will you be happy to modify a file somewhere whenever you want to make changes to it?
[eluser]Colin Williams[/eluser]
Take care of it in the constructor of Controller. Or, subclass Controller with MY_Controller, then subclass MY_Controller with your Main controller, and do the ops in MY_Controller's constructor. Search "MY_Controller" for more info
[eluser]jedd[/eluser]
Colin - you gotta be a bit faster than that if you want to beat me to the easy forum-question cherry-picking picnic! jasv - found a possibly more pertinent thread for you to read through : [url="http://ellislab.com/forums/viewthread/109698/"]Code Duplication Advice[/url]
[eluser]Unknown[/eluser]
Hi, thanks everyone for the fast response So, let me see if I understood. What i should do is to create a class MY_Controller (that extends Controller i guess), where i'm going to load the menu view, am i right? Thanks again
[eluser]gtech[/eluser]
kinda, what you do is in MY_Controller (which extends controller) you create a function that will load a view (lets call it template_v) In the template_v view you can then load two views one being the menu, and another the view with the main content. When you create controllers you extend MY_Controller and everytime you want to load a view you just call the $this->template_view(<VIEW NAME>,<VIEW ARRAY DATA>). function in MY_Controller Code: class MY_Controller extends Controller { template_v: Code: <DIV ID="side_menu"> one handy thing to know is once you load a view with a data array, the variables are available in every other view you load in the same request. So in the example above 'username' and 'iduser' will be available in the menu_v view (and the main content view). anyway when you call template_view in your extended controllers you pass it the view name and a data array like you do in the $this->load->view() function.
[eluser]gtech[/eluser]
^ attachment to above post ^ Oh dont forget when you create MY_Controller.php it needs to live in the system\application\libraries directory It can be called anything so long as it starts with MY_ in your extended controllers (the code which lives in the controllers directory) you can then do somthing like Code: class Test extends MY_Controller {
[eluser]crispinatari[/eluser]
really? but if its a controller then shouldn't it be in the controller folder?
[eluser]Colin Williams[/eluser]
No, crispinatari. It's a special case. Read http://ellislab.com/codeigniter/user-gui...asses.html |
Welcome Guest, Not a member yet? Register Sign In |