Welcome Guest, Not a member yet? Register   Sign In
Pages, subpages, subsubpages ... best way to achieve?
#1

[eluser]Unknown[/eluser]
Hello all! :-)

I'm not very proficient in CI and I'm in the midst of creating a website with it to learn more about it. Now I got stuck on this issue: subpages (eg. http://example.com/a_page/another_page/).

I combed through the forums and it seems routing is the best way. The site I'm creating won't be that big so at the moment I'm using just one controller and the standard CI-way of mapping the first URI segment to the controller method. Stripped down my controller looks like this:

Code:
<?php
class Kmx extends Controller
{
    function Kmx()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->library('session');
    }
    
    function _assemble($data = NULL)
    {
        $this->load->view("header_view", $data["header"]);
        $this->load->view("navigation_view");
        switch ($data["view"])
        {
            case "page_A":
                $this->load->view("home_view");
                break;
            case "page_B":
                $this->load->view("angebote_view", $data["content"]);
                break;
            default:
                $this->load->view("home_view");
        }
        $this->load->view("footer_view");
    }

    function index()
    {
        $data["header"]["title"] = "";
        $data["view"] = "home";

        $this->_assemble($data);
    }

    function page_A()
    {
        $data["header"]["title"] = " impressum";
        $data["view"] = "impressum";

        $this->_assemble($data);
    }

    function page_B()
    {
        $data["header"]["title"] = " agb";
        $data["view"] = "agb";
        $data["content"] = "Some content.";

        $this->_assemble($data, "agb");
    }
}
?>

As you can see I'm using an _assemble method which does some otherwise redundant stuff like adding a header etc. It also "injects" the optional content and loads the appropriate view. For me it works very well though I don't know if it's a good approach.

Anyway, how do you go around creating subpages? Is routing the way to go? It seems to me that adding a lot of entries to routing.php for a lot of subpages seems to be rather cumbersome. Or should I just take the second URI segment(the method's parameter) and switch through it? Is there any regular/standard way to get something like this done in just one controller?

Thanks for any input!

Regards
#2

[eluser]Pascal Kriete[/eluser]
Hey Therapiekind,

First off it looks like you're having some trouble with that controller, so let's refactor.

The view handling is much smarter than you're making it out to be, just make a template:

View - template.php
Code:
$this->load->view('header');
$this->load->view("navigation");

<div id="test">
some html for the fun of it
</div>

$this->load->view($view);

$this->load->view("footer");

Any data you pass to this view will be available in all subviews, so we've simplified things a lot.

Here's the new controller:

Controller - kmx.php
Code:
&lt;?php
class Kmx extends Controller
{
    function Kmx()
    {
        parent::Controller();
        $this->load->helper('url');
        $this->load->library('session');
    }

    function index()
    {
        $data["title"] = "";
        $data["view"] = "home";

        $this->load->view('template', $data);
    }

    function page_A()
    {
        $data["title"] = " impressum";
        $data["view"] = "impressum";

        $this->load->view('template', $data);
    }

    function page_B()
    {
        $data["header"]["title"] = " agb";
        $data["view"] = "agb";
        $data["content"] = "Some content.";

        $this->load->view('template', $data);
    }
}

And just to get the point across, let's say this is our agb view:

View - agb.php
Code:
<div id="all_data_is_sold">
You have no rights to any data on this site.  Oh and we own your house.  Owned.
</div>

<div id="stuff">
&lt;?=$content?&gt;
</div>

I'm having a hard time with that subpage stuff. You can get any url segment with $this->input->segment(n), and any stray segment will be passed to your controller as an argument.

example.come/blog/show/4

blog controller: view function
Code:
function show($id)
{
    echo $id; //prints 4
}

Welcome to CodeIgniter.
#3

[eluser]Unknown[/eluser]
Thanks for the answer, inparo!

I didn't even think about loading a view from within a view. And your template.php approach renders _assemble obsolete and cleans up my controller. That's great, too (though it sounded cool to have a function like this ... :rollSmile.

Regarding subpages: I think I will parse method parameters and load subpages accordingly.

However I'm still curious how others accomplish that.




Theme © iAndrew 2016 - Forum software by © MyBB