shruthi
shruthi

Reputation: 119

How to give URL in codeigniter route file

I want to display my page url like this below

For Home page (after login)

www.domain.com/username

For Aboutme page

www.domain.com/username/about

My route file line

This one worked for home page

$route['(:any)'] = "pages/userList";    //This worked for home page

But This one not worked, This page also goes for homepage

$route['(:any)/about'] = "pages/about";   //Not worked

Upvotes: 1

Views: 789

Answers (2)

Tpojka
Tpojka

Reputation: 7111

Issue is because of precedence of routes in APPPATH.'config/routes.php' file. Docs says next:

Routes will run in the order they are defined. Higher routes will always take precedence over lower ones.

Obeying that rule, very specific routes should appear first and most general routes need to be at the end of file. In your case it would be like:

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

Upvotes: 0

Zaragoli
Zaragoli

Reputation: 686

(:any) will catch everything, so it shouldn't be the first rule.

It would be better to use any segment before username, for example:

www.domain.com/user/username then rule is:

$route['user/(:any)'] = "pages/userList/$1";

It will match for: www.domain.com/user/adam, www.domain.com/user/bob, etc...

for this: www.domain.com/username/about

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

It will match for: www.domain.com/about/adam, www.domain.com/about/bob, etc... In your controller you can get the username:

$username = $this->uri->rsegment(3);

Upvotes: 1

Related Questions