1
votes

I'm just learning about codeigniter and am stumped by the following:

I have these routes in my routes file:

$route['(:any)'] = 'pages/view';
$route['news/create'] = 'news/create';
$route['news/(:any)'] = 'news/view/$1';
$route['news'] = 'news';
$route['default_controller'] = 'pages/view';

Yet when i load http://mysite/index.php/news," it loads the news page rather than 'pages/view,'. I thought that because I have the catchall route at the top, this page would also just load pages/views.

Where am i going wrong?

2
Catch all route should be on the end. But if you define a route for news, it will go to news.Iamzozo
Doesnt that kinda defeat the purpose of an order?Gareehc
No, it's logical: you define your "special" routes first, and if it's anything else go to pages/view. So if you want news go to pages/view just delete news routes.Iamzozo
But if what im saying is happening, then it seems that even if i define any first, it will still direct to the 'special' controller, which is confusing me because i thought order mattered, this seems to suggest that it doesnt. I understand that i could just delete the news routes, im just trying to understand. Given that the any toutes is defined first and order supposedly matters, i would imagine that the any routes is called when the url contains news despite there being a news route.Gareehc
Yes, sorry, I was wrong. Tested with CI2 and 3, and seems the order doesn't have any impact.Iamzozo

2 Answers

0
votes

The reason is :any doesn't match slash, at least since CI 3.0+ . So your first route rule is not a "catch all" any longer. http://www.codeigniter.com/user_guide/installation/upgrade_300.html?highlight=any#routes-containing-any

0
votes

any and num is important for routing in codeigniter.

we will use any for the string value such as order001.

we will use num for the integer value such as 001.

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

$1 is define for the first parameter.

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

for 2 parameter pass in url.