CodeIgniter routes - remove class name from url for only one class

I want to reassign this: index.php/pages/services

to this one index.php/services

.

How can i do this?

I tried this but it doesn't work :

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

      

== UPDATED ==

Is there any dynamic approach to this method? So every function inside this class will become a reassignment without classname

in url

?

+1


source to share


1 answer


To map www.domain.com/services

to pages/services

, you would like:

$route['services'] = 'pages/services'

      

If you want to map www.domain.com/whatever

to pages/whatever

and whatever

, has multiple options, and you have multiple controllers, then you should:

// create rules above this one for all the controllers.
$route['(:any)'] = 'pages/$1'

      



That is, you need to create rules for all of your controllers / actions, and the latter should be a catch-all rule as above.

If you have too many controllers and want to resolve this particular route, in routes.php file it is safe to:

$path = trim($_SERVER['PATH_INFO'], '/');
$toMap = array('services', 'something');
foreach ($toMap as $map) {
    if (strpos($path, $map) === 0) {
       $route[$map] = 'pages/'.$map;
    }
}

      

Note. Instead, $_SERVER['PATH_INFO']

you can try $_SERVER['ORIG_PATH_INFO']

or any other component that will give you the full url.
Also, the above is untested, this is just an example to get you started.

+3


source







All Articles