Lukman Azhari
Lukman Azhari

Reputation: 85

CodeIgniter - how to remove class name in url route

I have http://localhost/CIrbbps/index.php/class_name/method_name . how to remove class name?, so i can get just http://localhost/CIrbbps/index.php/method_name . thank you

Upvotes: 3

Views: 385

Answers (2)

Nitish Kumar Diwakar
Nitish Kumar Diwakar

Reputation: 661

try this:

$route['(:any)'] = "account/$1";

Upvotes: 1

Parth Chavda
Parth Chavda

Reputation: 1829

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.

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

Upvotes: 2

Related Questions