Uffo
Uffo

Reputation: 10056

Codeigniter dynamic url for pages breaks routes

So in my routes.php file as the LAST route I have this:

$route['(:any)'] = '/page/index/$1';

This is used for dynamic pages url like foo/dynamic-page the problem here is that if I have a controller called something.php and the route will be foo/something and it's not manually declared in the routes.php file it will return a 404 because I think it will hit the (:any) route.

The question will be: How to make the router init a static route first before going to check dynamic page route

Upvotes: 0

Views: 275

Answers (2)

Kaleem Ullah
Kaleem Ullah

Reputation: 7059

Try This:

$route[ 'default_controller' ]  = 'main';
$route[ '404_override' ]        = 'error404';

require_once( BASEPATH .'database/DB'. EXT );
$db =& DB();
$query = $db->get( 'app_routes' );
$result = $query->result();
foreach( $result as $row )
{
    $route[ $row->slug ]                 = $row->controller;
    $route[ $row->slug.'/:any' ]         = $row->controller;
    $route[ $row->controller ]           = 'error404';
    $route[ $row->controller.'/:any' ]   = 'error404';
}

Answer Source

OR this

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

any thing you type on the url will proceed to pages/view/$1 the $1 here is the param you wants to pass to a controller/method

$route['login/(:any)'] = 'show/jobs/$1';

you are telling CI that anything that goes to login with any parameter like login/user will proceed to your show/jobs/usr (:any) will match all string and integer if you use (:num) it will only match integer parameters like

$route['login/(':num')'] = 'show/jobs/$1'

Upvotes: 0

Rooneyl
Rooneyl

Reputation: 7902

Routes are processed in the order listed.

Note: Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

So if you (:any) is your catch all before throwing a page not found, just ensure it is placed before the (:any).

E.g.

$routes['foo/something'] = 'something/index';
$routes['(:any)'] = '/page/index/$1';

Upvotes: 1

Related Questions