Reputation: 946
I am using cakephp 1.3. here's my problem:
I have a controller named "learns" and a method named "classroom".
To access the classroom method, I use this link: http://www.url.com/learns/classroom/15
I wanted it to be like this: http://www.url.com/class/15
And this is how I setup the routes:
Router::connect('/:class/:id', array('controller' => 'learns', 'action' => 'classroom'), array('id' => '[0-9]+'));
I don't really know why its not working though. I read through the documentation and I just copied this solution from the cookbook..
Thanks for the help in advance.
Upvotes: 0
Views: 118
Reputation: 71
I don't see anything wrong with your Router statement. Though I am not sure if you actually wanted /:class/:id instead of "/class/:id". See the difference? A Colon is missing in the 2nd version.
This tells the Router that any request that begins with /class/[an-id] should be mapped to whatever your rule is. Where as having it as /:class means you are passing an argument to the router. It can be anything /foo/15 or /bar/15 and these arguments will be available in your $this->params['class'] and $this->params['id']., assuming this rule -> /:class/:id
In your question you state that "I don't really know why its not working". Please avoid these kind of statements as it does not say any thing about the actual problem.
Instead tell us what were you expecting? And what did you see instead? Was it an error? Or a Warning? If you see something else entirely, for example a different action got executed, it is probably due to the fact how routers actually work. If you had a greedy route and a normal route like this:
/users/* and /users/:id
The second url will not be matched for any request as /users/* is greedy and will hog all the requests to itself, unless the first routing rule returns false.
If this is your situation I would suggest you read up on how to write custom route classes. In summary custom route classes try to make a greedy route less greedy. For a better explanation here is an excellent article by mark story: http://mark-story.com/posts/view/using-custom-route-classes-in-cakephp
Routing in cakephp is one of the most confusing topics and might take a while before you get your head around it. Cookbook is your best friend. Read and Re-read everything until you are sure what each routing element does in a routing rule.
Upvotes: 1