Question

In my CodeIgniter installation, my routes file is currently:

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

//routes
$route['about']              = 'page/view/about';
$route['(:any)']             = 'page/view/$1';
$route['default_controller'] = "page/view";

My question is - do I need to make a new $route call each time I have a new page, or is there a way to do it automatically? My page controller will be for my static pages...home, about, contact, faq, etc.

Do I need to specify each static page?

This might also stem to when I get to the registration part of the code. How will I automatically give a user their own route?

Thanks

Was it helpful?

Solution

One way to avoid manually setting routes for each page is to create a Page controller and route all uri's to this controller.

Routes.php:

// Default controller
$route['default_controller'] = "page/index";

// Page controller catch all
$route['(:any)'] = 'page/view/$1';

The order of your routes.php file is important, and these should be the last two lines in the file. If you have other controllers (i.e. News/Blog/Products/Whatever) their routes should go above these two routes.

Page.php

class Page extends CI_Controller {

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

    public function index()
    {
         // This will be where your load your top page (homepage)
    }

    public function view($uri)
    {       
         // This will be where you load all other content pages (about/info/contact/etc)
         echo $uri;
    }   
}

Obviously this is very basic, but it gives you an idea of how to achieve automatic routing for Pages. Once you know the uri, you can use that to pull information about that page from a csv/database/textfile and then load the appropriate view for that uri.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top