CodeIgniter Forums
Routing for ALL urls - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forumdisplay.php?fid=20)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forumdisplay.php?fid=23)
+--- Thread: Routing for ALL urls (/showthread.php?tid=3148)



Routing for ALL urls - El Forum - 09-12-2007

[eluser]@li[/eluser]
Hi,

I want to give my app's admin the ability to add new pages to their site, and also to provide a file-name for the page. For example they would:

1) Click 'Create new Page'
2) Type in the HTML code/content that the page would display.
3) Type in 'sign-up.html' or 'signup.php' in the file-name field. This would make the URL to the new page be: http:/www.site.com/signup.html or http://www.site.com/signup.php

Obviously since signup.html wouldn't actually exist, I want the request to be routed to pages/[file-name]. I was thinking of just adding this route:
Code:
$routes[':any/']='pages';

But obviously, it would send ALL requests to the pages controller, while I only want those requests to be routed for which an appropriate controller cannot be found. I.E if a controller and method can be found for the URL, then they should be called normally, otherwise the pages/ controller should be called.

Is there anyway to do this?


Routing for ALL urls - El Forum - 09-12-2007

[eluser]coolfactor[/eluser]
There's no easy way to do it, but I'd extend the Router class with a MY_Router class and then configure it to check for an existing controller, else modify the uri to match your dynamic page handler.


Routing for ALL urls - El Forum - 09-12-2007

[eluser]@li[/eluser]
Cool,
which function exactly would I have to change in the Router class? I've looked into this before but couldn't figure out which one I'll have to change.


Routing for ALL urls - El Forum - 09-12-2007

[eluser]sparkling tux[/eluser]
There IS an easy (easier at least) way.

In the routes.php do something like this

Code:
$exclude =     array(
                    '.',
                    '..',
                    'index.html',
                    'pages.php'
                );

$handle = opendir(APPPATH.'controllers/');

while ( false !== ($file = readdir($handle)))
{
    if ( !in_array($file, $exclude) )
    {
        if ( !is_dir(APPPATH.'controllers/'.$file) )
        {
            $file = substr($file, 0, strlen($file) - 4);
        }
        
        $installed_modules[] = $file;        
    }
}

closedir($handle);

foreach ($installed_modules as $module)
{    
    // e.g. index.php/(en|ru|..)/news(/param1/param2/...)
    $route["(.{2})/$module(.*)"] = $module . "/$1" . "$2";
    
    // e.g. index.php/rss/news(/param1/param2/...)
    $route["rss/$module(.*)"] = $module . "/rss" . "$1";
    
    // e.g. index.php/(en|ru|..)/rss/news(/param1/param2/...)
    $route["(.{2})/rss/$module(.*)"] = $module . "/$1/rss" . "$2";
    
    // e.g. index.php/news    (/param1/param2/...)
    $route["$module(.*)"] = "$module" . "$1";
}

It's not my invention - I saw this approach somewhere here.


Routing for ALL urls - El Forum - 09-13-2007

[eluser]xwero[/eluser]
[quote author="@li" date="1189642266"]Hi,

I want to give my app's admin the ability to add new pages to their site, and also to provide a file-name for the page. For example they would:
[/quote]

I can understand you want to give the admins the possibility to add pages to their site but i would not let them choose the file extension. I would add this to the chosen file name, this way you have a bit more control and you would only have to check the file name not the extension. Or if you want to make nice urls just don't use the file extension.

I'm wondering if you will allow to have directories in their urls like profile/me.html, profile/you.html