开发者

Consistent theme and content in CodeIgniter

I'm working on a basic website using CodeIgniter to better understand the framework and improve upon my currently limited web dev skills. I've been searching and reading quite a bit of documentation but haven't found exactly what I'm looking for (or at least I don't think).

My site will have a consistent header, navigation and footer. Essentially the only content that will change via the subnav is some text and or images between the header and footer. This is pretty typical of a simple website, but I'm trying to figure out how to approach this in the context of views / controllers.

From my understanding in this scenario you want to create a "theme" folder within views that contains a "main" view which is loaded with each page. I'm also under the impression that with the ability to load multiple views in a single controller, I'll really only need one "home" controller that loads the 开发者_如何学JAVA"main" view alongside whatever view is associated with the nav. So for example, Nav: Home | About | contact. I click "About" and through the appropriate home controller method I load the main + about views.

This is the approach I'm currently trying to take, but I figured while attempting this I'd get some feedback.

I'm not necessarily looking for a step by step tutorial, but more so the accepted or most common approach. Much appreciated, and I apologize in advance if this was staring me in the face 2 posts down.

Thanks -Jay


You could try using the Template library. Its what I use on every codeigniter project I do and I love it.

Template


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

You will want to have it set up like this:

$route['(:any)'] = "home/$1";
$route['default_controller'] = "home";

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.

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

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 $this->load->view(header) and $this->load->view(footer) etc. for each function.

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:

 private function viewloader($view, $data) {
         $this->load->view('includes/header', $data);
         $this->load->view('includes/nav', $data);
      $this->load->view($view, $data);
         $this->load->view('includes/footer', $data); 
 }

...or by using codeigniter's built in underscore before the function that makes it not accessible via the URL

 function _viewloader($view, $data) {
         $this->load->view('includes/header', $data);
      $this->load->view($view, $data);
         $this->load->view('includes/footer', $data); 
 }

Then your functions for each page would look something like this:

 function about()
 {
  /* put any application logic here */
  $this->viewloader('about', $data);
        /* or $this->_viewloader('about', $data); 
           if you went with CI's style visibility */
 }

 function contact()
 {
  /* put any application logic here */
  $this->viewloader('contact', $data);
        /* or $this->_viewloader('contact', $data); 
           if you went with CI's style visibility */
 }

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.

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.

Here is what it might look like at the end:

<?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->viewloader('home', $data);
 }

 function contact() {
  $data['title'] = "Contact Us"
  $this->viewloader('contact', $data);
 }
 function about() {
  $data['title'] = "About Us"
  $this->viewloader('about', $data);
 }

 private function viewloader($view, $data) {
  $this->load->view('includes/header', $data);
  $this->load->view('includes/nav', $data);
  $this->load->view($view, $data);
  $this->load->view('includes/footer', $data);
 }

}


I use my own mvc, but the concept is still the same. What I have started doing is creating a base controller class with the following methods.

class base_controller {

    function assemble_display() {
        $this->display_view('head');
        $this->display_main_content();
        $this->display_view('foot');
    }

    function display_main_content() {
        $this->display_view('home');
    }

}

Then, in subsequent controllers that extend this base class, I overwrite the display_main_content() method.

class search extends base_controller {

        function display_main_content() {
            $this->display_view('search');
        }

}

This way I can call assemble_display() in any controller, and it will show the base head and foot but with the correct main content.


CodeIgniter favours including the view parts of your pages sequentially from your controller functions. In the past, I've added my own baseclass to CI that implements a set of custom view functions:

  • show($tab, $body, $data) - display a view using header/body/sidebar/footer parts. Sidebar and body are pulled from the current $tab view folder.
  • _print($tab, $body, $data) - a show(); variant for printing (sets up CSS, removes parts not needed for print). This could be done with CSS alone, but I've had clients needing special server-side mojo for printing.
  • widget($type, $id) - send a view chunk (generally for js/Ajax)
  • api($type, $template) - send API data by type (JSON, XML, etc.).

This approach has worked well for a number of larger projects, but there are other ways. CodeIgniter's fork Kohana, for example, inverts the pattern, and places the page composition logic in templates. These templates perform the function of the base controller in a more natural way, so instead of multiple controller functions for different types of pages, you use different templates. The result is a set of view files like:

  • app/main-template.php
  • app/login-template.php
  • app/print-template.php
  • app/reports/wide-template.php
  • widgets/control-template.php
  • api/soap-template.php
  • and so on.

The Kohana approach can be applied to CodeIgniter as well, and is more common in other frameworks (like Rails, Cake, Zend, etc.).

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜