Welcome Guest, Not a member yet? Register   Sign In
Quick route question
#1

[eluser]Unknown[/eluser]
I'm building a site that has a few Controllers for hard-coded 'modules' as well as a fairly simple 'CMS' style pages Controller.

My idea was to have URL paths like:
Code:
site.com/module1/action/...
site.com/module2/action/...
Which route in the normal controller/function/param fashion and anything else is a 'CMS' page:
Code:
site.com/about-us
site.com/contact-us
Which would map to site.com/pages/page/about-us, site.com/pages/page/contact-us.

So far I've come up with something like this in routes.php:
Code:
$route['module1'] = 'module1';
$route['module1/(.*)'] = "module1/$1";
...
$route['(.*)'] = "pages/page/$1";
Am I on the right track here?
#2

[eluser]Phil Sturgeon[/eluser]
Yea that'll work fine but it does mean you will have to hardcode in more routes each and every time you add a new page.

You could try something like I did on a similar project, hijack the exceptions show_404 functionality to send it to the pages controller if there is no module/controller of that name, then use pages to create a sexier looking 404 page. Win-win that way!

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

class MY_Exceptions extends CI_Exceptions
{
    function show_404($page = '')
    {
        global $CFG;
        
        // Removes the name of the script from the request uri, so we can grab segment1
        $uri_string = str_replace($_SERVER['SCRIPT_NAME'], '', $_SERVER['REQUEST_URI']);
        list($nothing, $segment1) = explode('/', $uri_string);
        
        // Redirect to a CI controller where we have more API access
        header('Location: '.$CFG->item('base_url').'index.php/pages/check/'.urlencode($segment1));

/*
        log_message('error', '404 Page Not Found --> '.$page);
        echo $this->show_error($heading, $message, 'error_404', $title);
        exit;
*/
    }

}    
?>




Theme © iAndrew 2016 - Forum software by © MyBB