Welcome Guest, Not a member yet? Register   Sign In
Getting my head around it - controllers and views
#1

[eluser]Samuurai[/eluser]
Hi,

I've just started playing with CodeIgniter.. I like how free I am to combine what I know about PHP with MVC architecture. I played with Rails for a bit, but I felt far too locked down.

I'm just trying to figure out how to incorporate a header and footer into my page.

At the moment, my controller looks like this:

Code:
function index()
    {
        $data['title'] = "Search Results";
        $data['heading'] = "Found Results";
        $this->load->view('header', $data);
        $this->load->view('search');
        $this->load->view('footer');
        
    }

the header is simply a header.php file in the views directory. However, I wanted to use the anchor() function in the header view. Of course this won't work because it doesn't have a corresponding controller.

What is the best way to incorporate a header and footer?

Cheers!
#2

[eluser]sjmiller85[/eluser]
Your view is loaded inside of the controller, so any libraries or helpers (or any other things like plugins) that are loaded inside of the controller "roll-over" into the view that is using it, so this should be what you need:

Code:
function index()
    {
        $this->load->helper('url');
        $data['title'] = "Search Results";
        $data['heading'] = "Found Results";
        $this->load->view('header', $data);
        $this->load->view('search');
        $this->load->view('footer');
        
    }
#3

[eluser]jedd[/eluser]
Quote:What is the best way to incorporate a header and footer?

Well, [url="http://ellislab.com/forums/viewthread/109698/"]this thread[/url] contains a way to incorporate a header and footer pretty painlessly.
#4

[eluser]Samuurai[/eluser]
[quote author="sjmiller85" date="1243463379"]this should be what you need:[/quote]

That's precisely what I needed.. thanks! I added it to autoload.

[quote author="jedd" date="1243477589"][url="http://ellislab.com/forums/viewthread/109698/"]this thread[/url] contains a way to incorporate a header and footer pretty painlessly.[/quote]

The idea of having a function seems like a smart move.. Is that the appraoch you were referring to?

I do have one other query... if I create a search widget and want to include it in the header, since the header has no controller, how do I include it so that the search widget has access to it's controller?

Thanks for the replies, everyone!
#5

[eluser]jedd[/eluser]
[quote author="Samuurai" date="1243521825"]
The idea of having a function seems like a smart move.. Is that the appraoch you were referring to?[/quote]

Well, there's a few things implied in there .. or at least, should have been implied.

First, [url="http://ellislab.com/codeigniter/user-guide/general/core_classes.html"]extend your controller[/url] - it's an incredibly useful feature, particularly for what we're talking about here (having common view snippets introduced for every controller to access).

Second, generate your common view bits using actual real life views (not code within the controller) but obviously called from your extended controller class. I use views/common/header.php as an example. Use the third parameter of $this->load->view() - this is discussed at the very bottom of the [url="http://ellislab.com/codeigniter/user-guide/general/views.html"]CI guide to views[/url] - to facilitate this.

Quote:I do have one other query... if I create a search widget and want to include it in the header, since the header has no controller, how do I include it so that the search widget has access to it's controller?

Why does the widget need to access its controller?

If you generate the widget as a view snippet (as above) then it can just be dumped into the default or primary view file later as a string, and echoed at the appropriate place. Usually search functions present as forms that take you to a specific controller/method combo, so they shouldn't need to be aware of the controller they're nominally 'in'.
#6

[eluser]Samuurai[/eluser]
Ok, I got it working! I found this extremely helpful thread.

I now understand it a lot better... you're right, the view doesn't need the controller, it works the other way around.

Thanks for everyone's very helpful posts!




Theme © iAndrew 2016 - Forum software by © MyBB