Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>So you want to make sure that you understand routes in order to make sure that everything maps to the first segment of your URL, since you really only need one controller. This is in the application/config folder in a file called routes.php</p> <p>You will want to have it set up like this:</p> <pre><code>$route['(:any)'] = "home/$1"; $route['default_controller'] = "home"; </code></pre> <p>So that matches anything (:any) in the first segment and routes it to home/$1 (controller/function)... and then if there is nothing in the first segment it maps it to the home/(index function which is implied). Take a look at the documentation for the routes file so you can get a sense of what all you can do with it (you can run regular expressions, advanced routing, etc) it's really important to know.</p> <p>This way in your home.php controller (controllers/home.php or whatever you want to call it) you can just have functions that match up with your url's</p> <p>What is being mentioned in other posts about extending the base controller isn't really necessary in this instance. But it is nice to have a function that does view loading for you so that you don't have to repeat <code>$this-&gt;load-&gt;view(header)</code> and <code>$this-&gt;load-&gt;view(footer)</code> etc. for each function.</p> <p>What I like to do is to create an includes folder with my header, footer, nav, etc. create a function that does just that... with private accessibility either like this:</p> <pre><code> private function viewloader($view, $data) { $this-&gt;load-&gt;view('includes/header', $data); $this-&gt;load-&gt;view('includes/nav', $data); $this-&gt;load-&gt;view($view, $data); $this-&gt;load-&gt;view('includes/footer', $data); } </code></pre> <p>...or by using codeigniter's built in underscore before the function that makes it not accessible via the URL</p> <pre><code> function _viewloader($view, $data) { $this-&gt;load-&gt;view('includes/header', $data); $this-&gt;load-&gt;view($view, $data); $this-&gt;load-&gt;view('includes/footer', $data); } </code></pre> <p>Then your functions for each page would look something like this:</p> <pre><code> function about() { /* put any application logic here */ $this-&gt;viewloader('about', $data); /* or $this-&gt;_viewloader('about', $data); if you went with CI's style visibility */ } function contact() { /* put any application logic here */ $this-&gt;viewloader('contact', $data); /* or $this-&gt;_viewloader('contact', $data); if you went with CI's style visibility */ } </code></pre> <p>So now as you can see, that viewloader function now loads the views of the header, nav, whatever $view is and then footer all at once...pretty nice. </p> <p>You can also remember that it is possible to load any views you need in the view files themselves (nested views), they don't always have to be loaded from the controller, although it is good to keep it that way so you don't have to edit individual view files if you want to make a major change.</p> <p>Here is what it might look like at the end:</p> <pre><code>&lt;?php if (! defined('BASEPATH')) exit('No direct script access'); class home extends CI_Controller { //php 5 constructor function __construct() { parent::__construct(); } function index() { $data['title'] = "Welcome To Our Site" $this-&gt;viewloader('home', $data); } function contact() { $data['title'] = "Contact Us" $this-&gt;viewloader('contact', $data); } function about() { $data['title'] = "About Us" $this-&gt;viewloader('about', $data); } private function viewloader($view, $data) { $this-&gt;load-&gt;view('includes/header', $data); $this-&gt;load-&gt;view('includes/nav', $data); $this-&gt;load-&gt;view($view, $data); $this-&gt;load-&gt;view('includes/footer', $data); } } </code></pre>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload