CodeIgniter Routes - remove a classname from URL for one class only

前端 未结 1 1946
攒了一身酷
攒了一身酷 2021-01-16 05:20

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

How I suppose to do that ?

I tried th

相关标签:
1条回答
  • 2021-01-16 05:35

    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.

    0 讨论(0)
提交回复
热议问题