CodeIgniter Routes - remove a classname from URL f

2020-03-30 04:31发布

问题:

I want to remap this: index.php/pages/services into this index.php/services.

How I suppose to do that ?

I tried this, but it's not working :

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

== UPDATED ==

Is there any dynamic approach to this method ? So every functions inside that class would become remap without the classname in url ?

回答1:

In order to map www.domain.com/services to pages/services you would go like:

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

If you want to map www.domain.com/whatever to pages/whatever and whatever has a few variants and you have a few controllers, then you would do :

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

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

If you have too many controllers and you want to tackle this particular route, the in your 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 of $_SERVER['PATH_INFO'] you might want to try $_SERVER['ORIG_PATH_INFO'] or whatever component that gives you the full url path.
Also, the above is not tested, it's just an example to get you started.