Jonathon
Jonathon

Reputation: 16283

CodeIgniter building a CMS with different page types

I've just started to get into MVC with PHP and have had a good mess about with the likes of CodeIgniter and CakePHP. I'm interested to find out what people's approaches to the following would be:

Normally when I have built a website with a CMS in the past I have approached it by having a core URI table in my database. Each URI is unique and represents a page on my website (e.g. www.example.com/about would reference a record in my URI table with 'about' as the unique URI). The URI table also contains a 'type' column which tells the system what type of page it is (e.g. splash, basic, gallery or whatever). Each 'type' has a corresponding table in my database with all the data for records of that type in them (e.g. I would have tables: basic, gallery and splash). The type also tells the system which template/pagehandler to load which in turn does what it needs to do for each page type.

So if you go to www.example.com/about, my system looks in my URI table for a record with URI 'about', finds it's type to be 'basic' so it loads the basic template/pagehandler which uses the basic table in my database to load and render the page. In the CMS I follow a similar approach, I'll have add/edit forms for all of the different types in my page manager.

I was wondering how you would approach this using an MVC framework such as CodeIgniter? I essentially want to have different controllers for each type of page on both the front and back. However, when someone get's to my site, they will end up on a URI with a single level so I need to check the type of the page and pass off to the correct controller. Is there a way you would recommend of checking the type of each page and then loading the relevant controller to do the rest of the work?

Upvotes: 1

Views: 873

Answers (2)

Jonathon
Jonathon

Reputation: 16283

My approach eventually was to extend the _parse_routes() method of the Router class to check the database for any records matching the current uri and set the request with the corresponding value from the database.

<?php  if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class MY_Router extends CI_Router {

    function __construct() {
        parent::__construct();
    }

    function _parse_routes() {
        require_once( BASEPATH .'database/DB'. EXT );
        $db =& DB();

        $routes_table_exists = $db->query("SHOW TABLES LIKE 'routes';");
        if ($routes_table_exists->num_rows > 0) {
            $uri_routes = $db->get_where('routes', array('uri' => $this->uri->uri_string()));

            if ($uri_routes->num_rows > 0) {
                $row = $uri_routes->result()[0];
                if (isset($row->request)) {
                    return $this->_set_request(explode('/', $row->request));
                }
            }
        }

        parent::_parse_routes();
    }

}

Whether or not it's the best approach to take it seems to work so far.

Upvotes: 2

cartalot
cartalot

Reputation: 3148

Usually its a combination of Routes and naming your controllers. so for example you have an About page, and you don't need a separate About controller. Lets say you have a general Pages controller, and then a view($page) method to retrieve and show the page.

example.com/about
$route['about'] = "pages/view/about";

if you just have a few pages there are advantages to hard coding the routes - it protects your database. but otherwise taking an example from the tutorial

$route['default_controller'] = 'pages/view';
$route['(:any)'] = 'pages/view/$1';

this does the same thing but now it will take example.com/anything can go here

Versus something like a contact page - where you probably want to have a separate controller called Contact, because you will need to validate the contact form, add it to a database, email it, show a response, show the form again if did not validate, etc So then you can just do a simple link to show the contact form: example.com/contact

the contact form submits to: example.com/contact/submit

more about Routes http://ellislab.com/codeigniter/user-guide/general/routing.html

and definitely look at the tutorial it will give you more examples about routes http://ellislab.com/codeigniter/user-guide/tutorial/index.html

Upvotes: 0

Related Questions