Welcome Guest, Not a member yet? Register   Sign In
Show Menu on every Page...
#1

[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
class Main extends Controller{
    var $us;
    var $idus;
    function Main(){
        parent::Controller();
        $this->load->helper('url');
        $this->load->library('Session');
        $this->us = $this->session->userdata('DX_username');
        $this->idus = $this->session->userdata('DX_user_id');
    }
    
    function index(){        
        $data['username'] = $this->us;
        $data['iduser'] = $this->idus;
        $this->load->view('Main/index',$data);
    }
}

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.
#2

[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?
#3

[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
#4

[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! Wink

jasv - found a possibly more pertinent thread for you to read through : [url="http://ellislab.com/forums/viewthread/109698/"]Code Duplication Advice[/url]
#5

[eluser]gtech[/eluser]
did someone say there are cherries going round?
#6

[eluser]Unknown[/eluser]
Hi, thanks everyone for the fast responseBig Grin

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 Big Grin
#7

[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 {
    function template_view($viewpath,$data) {
        $data['username'] = $this->us;
        $data['iduser'] = $this->idus;
        $data['view_path'] = $viewpath;
        $this->load->view('Main/template_v',$data);
    }
}

template_v:
Code:
<DIV ID="side_menu">
      &lt;?php $this->load->view('Main/menu_v');?&gt;
</DIV>
<DIV ID="main_content">
      &lt;?php $this->load->view($view_path);?&gt;
</DIV>


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.
#8

[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 {
  function index()
  {
    $pagedata = array('viewvar'=>'Hello World');
    $this->template_view('viewname_v',$pagedata);
  }
}
#9

[eluser]crispinatari[/eluser]
really? but if its a controller then shouldn't it be in the controller folder?
#10

[eluser]Colin Williams[/eluser]
No, crispinatari. It's a special case. Read http://ellislab.com/codeigniter/user-gui...asses.html




Theme © iAndrew 2016 - Forum software by © MyBB