Reputation: 169
Hello I have this url 127.0.0.1/link/index/25, my default controller it´s link and when I use 127.0.0.1 I can see the index of link controller but when i pass an argument in the url like 127.0.0.1/25 doesn´t work I must to use the complete url, how i could improve it?
I try to add a static route in the route.php but it doens´t work
$route['index/(:num)'] = 'link/index/$1';
Upvotes: 0
Views: 371
Reputation: 1171
You cannot have a route that does not say what the route is. How will CI know if it is supposed to be a controller or the default controller with an argument.
When you go to 127.0.0.1 you get the default link controller. To link to a controller with an argument you need to use 127.0.0.1/link/index/25
As for tidying up URLs, there are different ways to approach this. What do you want your URLs to look like? The first thing is to change your method away from index to, say, mywebpage, and change the default controller in the routes config file.
Or you can use routing to route prettier URLs, or you can use htaccess. But you cannot pass an argument to an unnamed controller. And your default controller cannot be in a sub-folder for the same reason.
Hope that helps,
Paul
Upvotes: 1
Reputation: 2158
There is one mistake,
You have to remove index $route.
$route['(:num)'] = 'link/index/$1';
For more details please read about implicit and explicit routing in codeigniter. And Also read this answer --> .how-to-set-dynamic-route-to-use-slug-in-codeigniter
Upvotes: 1
Reputation: 4534
You must use a rule to match the URL in the desired form. In your case this would be:
$route['(:num)'] = 'link/index/$1';
This being said, you may want to use some prefix in your URLs, not directly the numeric ID. It may help you in the future if your website grows. Something like http://127.0.0.1/link/25
and the route for this would be
$route['link/(:num)'] = 'link/index/$1';
Upvotes: 1
Reputation: 16117
If you want URL like 127.0.0.1/25
than your route should be:
$route['(:num)'] = 'link/index/$1';
Or if you want to use route as same $route['index/(:num)'] = 'link/index/$1';
, than your link should be:
127.0.0.1/index/25
But note that, this route $route['(:num)']
will change all other URLs, you must need to use route as same you are using just change the URL as
127.0.0.1/index/25
Side Note: Other solution is htaccess.
Upvotes: 1