简体   繁体   中英

CodeIgniter Routes: do I need to make an entry for each individual page?

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

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 (ie 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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM