Dimas
Dimas

Reputation: 67

How to rewrite URL with Codeigniter?

In Codeigniter how to rewrite URL?

I already tried to change the route like this :

$route['backend/user/profile/(:num)/'] = 'backend/user/profile/$1/$2';

But still nothing could change.

I have url : http://pa.ig/backend/user/profile/204/disabled
Url Expected : http://pa.ig/backend/user/profile/204

Please help me to fix this. Thank you.

Upvotes: 0

Views: 63

Answers (2)

H45H
H45H

Reputation: 1017

if your last param is string you can use (:any) for that

$route['backend/user/profile/(:num)/(:any)']  = 'backend/user/profile/$1';

Upvotes: 1

Sofyan Thayf
Sofyan Thayf

Reputation: 1328

if the second parameter is optional, you can try this

in your routes:

$route['backend/user/profile/(:num)'] = 'backend/user/profile/$1';
$route['backend/user/profile/(:num)/(:string)'] = 'backend/user/profile/$1/$2';

and in your controller, give default value for the second parameter of your function

public function profile( $id , $stat = 'disabled' ) {
  // your code ..
}

so, if the second parameter ($stat) ommitted in URL, it will get disabled value

Upvotes: 0

Related Questions