Ben
Ben

Reputation: 21625

How to structure a dynamic website with Codeigniter

I have some experience with php, but I just recently started learning Codeigniter. I used to have a website with a fixed navigation pane and sidebar, but the main section of the site loaded dynamically based on a Get variable. It was basically

include head.php
include navbar.php
include sidebar.php
include the page requested from the get variable (home, about, contact, etc.)
include footer.php

I liked this because the entire site did not have to reload when the user navigated from page to page.

I can't figure out how do this with Codeiginiter. Should I be using a controller for each page or one controller with a function for each page? Do anyone know of a good tutorial that does something similar? All the tutorials I've seen reload the entire site for every page.

Edit: Essentially I want to do this but with Codeigniter

Upvotes: 2

Views: 5603

Answers (4)

Rachmat Yanuarsyah
Rachmat Yanuarsyah

Reputation: 36

in codeigniter i using template

first make template file in one folder with header.php, navbar.php, etc.

example : template.php

<?php 
 echo $this->load->view('header'); //load header
 echo $this->load->view('navbar');//load navbar
 echo $this->load->view('sidebar');//load sidebar
 echo $this->load->view($body); //load dynamic content
 echo $this->load->view('footer');//load footer
?> 

second in controller

function index(  ){
$data['body'] = 'home'; // cal your content
$this->load->view('template', $data);
}

Upvotes: 0

&#222;aw
&#222;aw

Reputation: 2057

In codeIgniter you can do that like this, you can load different views at the same time from your controller. for example:

for example in your navbar view you have a Contacts button in your menu that would look like this:

<a href='contacts'>Contacts</a>

In your controller:

public function contacts()
{
    $this->load->view('header');
    $this->load->view('navbar');
    $this->load->view('sidebar');
    $this->load->view('contacts_view');
    $this->load->view('footer');
}

So we're assuming here that you have the following views already that is ready to be loaded (header.php, navbar.php, sidebar.php, contacts_view.php, footer.php).

UPDATE: you don't need to have $_GET[] request, just provide the method name from your controller in the <a> anchor tag

Upvotes: 1

ToxaBes
ToxaBes

Reputation: 1587

do you really like to copy/paste many $this->load->view() to any controller function? It's a spaghetti code. You can try next: for example we have main.php controller as default controller. This main controller contain main function:

public function index()
    {
        ob_start();
        $this->load->model('mainmodel');  
        $data = $this->mainmodel->_build_blocks(); //return array with needed blocks (header, menu, content, footer) in correct order
        foreach ($data->result_array() as $row) {
            $this->load->module($row['block_name']);
            $this->name = new $row['block_name'];
            $this->name->index();
        }            
        ob_end_flush();
    } 

So, each other controller also have index() function which can dispatch actions depends on url segments, prepare params etc.

Footer controller as example (I use Smarty as template engine):

public function index()
    { 
               $this->mysmarty->assign('year', date("Y"));
               $this->mysmarty->view('footer');
               return true;
    }

Content controller will have:

public function index()
    {
        $name = $this->uri->segment(1, 'index');
        $act = $this->uri->segment(2, 'index');
        $this->load->module($name);
        $this->name = new $name;
        $pageData = $this->name->_show($act);
        if ($pageData)
        {
            $this->mysmarty->assign($name, $pageData);
        }
        $this->mysmarty->view($name);
    }

Thats mean what if you want to show http://site.name/page/contactus , we do next:

1) main.php start cycle by needed blocks

2) firstly we show header.tpl by header controller

3) then we show menu

4) then we call content controller which parse url, found what he should call _show() function in Page controller and pass action='contactus' to it. _show() function can contain some switch/case construction which show templates depends of action name (contactus.tpl in this case)

5) in the end we show footer template

In such case we have flexible structure. All controllers should have index() functions and all controllers who can be called in content should have _show($act) function. Thats all.

Upvotes: 1

Lee Avital
Lee Avital

Reputation: 542

Since it looks like you want relatively static content, but loaded dynamically you can do with one controller and (maybe) one method in the controller.

To do it with one method, do this in the welcome controller:

public page( $page_id ){
    // views/header.php
    $this->load->view( "header" );


    if( $page_id = "about" ){
      $this->load->view("about");     // views/about.php
    }

    else if( $page_id = "contact" ){
      $this->load->view("contact");  // views/contact.php
    }

    // views/footer.php
    $this->load->view("footer");
}

This takes a single get variable and figures out what page to load in between the header and footer.

This way www.yoursite.com/page/about will load the about page, www.yoursite.com/page/contact will load the contact page

Now, if you want to get rid of the /page part, you need to do some URL rerouting in application/config/routes.php

Alternatively you could use several methods in one controller:

public about(  ){
    // views/header.php
    $this->load->view( "header" );

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

    // views/footer.php
    $this->load->view("footer");
}


public contact(  ){
    // views/header.php
    $this->load->view( "header" );

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

    // views/footer.php
    $this->load->view("footer");
}

Now your URLs look nicer without routing, but you have to load the header/footer for every page.

Upvotes: 3

Related Questions