Routes: how to get mycontroller/id function same as mycontroller/action/id |
[eluser]dreamynsx[/eluser]
Hello, I've read routes documentation already, but it really does not have a lot of examples for what I'm trying to do. Please help. Code: // my sample class with index action How do I get it so that both requests like this work: www.site.com/mycontroller/index/3<-- this works by default, sets $id value to 3 www.site.com/mycontroller/3 <-- this fails with 404, since '3' is not valid action www.site.com/mycontroller/add <-- this works as expected www.site.com/mycontroller <-- this will work and load index action by default This is simply because I don't want to use url like www.site.com/cars/index/3 as I think having index at the end of every section looks funny. So www.site.com/cars/3 is what I want to work. Thanks!
[eluser]Majd Taby[/eluser]
From the user guide: $route['products/([a-z]+)/(\d+)'] = "$1/id_$2"; Which means, you probably want: $route['cars/[0-9]'] = "cars/index/$1";
[eluser]dreamynsx[/eluser]
Hi Newbie...thanks. $rote['cars/[0-9]' = "cars/index/$1"; This works such that the page is called correctly, but the $id digit isn't initialized. Its empty.
[eluser]marcoss[/eluser]
[quote author="dreamynsx" date="1192829171"] Code: // my sample class with index action You should rewrite that like this, Code: class MyController extends Controller Then you get http://site.com/cars/id/3.
[eluser]dreamynsx[/eluser]
Well the real idea for all this is that my index action will show a list of cars, and can accept a category Lets say I have 20 categories. I wouldn't want to create 20 different methods just to support this. So back to the function index($cat="") { ... } www.site.com/cars/index/sports <-- works (shows 'sport' cars) www.site.com/cars <-- works (just show default list) www.site.com/cars/sports <-- fails ...
[eluser]Majd Taby[/eluser]
Here's a conversation that happened in the irc channel (#codeigniter on irc.freenode.net), thought it would help clear things up: Quote:<1 - needs help> man it must just be me, but ive never gotten even the simplest routes to work
[eluser]marcoss[/eluser]
[quote author="dreamynsx" date="1192840619"]Well the real idea for all this is that my index action will show a list of cars, and can accept a category Lets say I have 20 categories. I wouldn't want to create 20 different methods just to support this. So back to the function index($cat="") { ... } www.site.com/cars/index/sports <-- works (shows 'sport' cars) www.site.com/cars <-- works (just show default list) www.site.com/cars/sports <-- fails ...[/quote] Again, you are missing the point here, it is about architecture, you should do something like this Code: class MyController extends Controller Now you have, http://site.com/cars/show/sports --> will list all cars on sports category. http://site.com/cars/show/sports/5 --> will get the car with id 5 in the sports category.
[eluser]dreamynsx[/eluser]
[quote author="marcoss" date="1192921895"] Now you have, http://site.com/cars/show/sports --> will list all cars on sports category. http://site.com/cars/show/sports/5 --> will get the car with id 5 in the sports category.[/quote] Actually I think your not understanding my full scenario, however your example should help me make it clearer. What if in your example, I want site.com/cars to show the list of all cars, and site.com/cars/sports to show list of all sports cars ? is this not possible without having the method 'show' in there? I do not want to have to do http://site.com/cars/show/sports. That extra show path seems uneccessary to me.
[eluser]heha[/eluser]
I'm facing the same problem wtih you, dreamynsx. But in Zaatar's reply has already showed the way to complete that. Try: $route['(cars/[a-z]+)'] = "cars/index/$1"; (Close route with bracket.) That's it. Anyway I still don't know why this work. Don't know very much with regular expression.
[eluser]easymind[/eluser]
http://xxx.xx/cars/1 runs the function called 1 inside your controller cars (so it will not start the index function and pass 1 as a argument) http://xxx.xx/cars/index/1 runs the index function inside your controller cars and passes 1 as the first argument As suggested to you, this can be done nicer by making a function called show, so you can leave out index in the url and replace it with show. Show will have to do what you want index to do. So you will get the url: http://xxx.xx/cars/show/1 The other suggestion was routing. So cars/1 goes to cars/index/1 or cars/show/1, whatever you like... Is this a simpler explanation of the suggested solutions above? |
Welcome Guest, Not a member yet? Register Sign In |