tags:

views:

99

answers:

1

Hello

I am working on cakephp and totally a newbie to php/cakephp. Can you please tell me what is wrong with my route configuration here?

Router::connect(
 '/news/:q/:page',
 array('controller' => 'news', 
       'action' => 'onDemand',
       'mode'=>'news',
       'page'=>1),
 array('pass'=>array('q','mode','page'),
       'page' => '[\d]+'));

When i access the page as /news/123 or /news/123/1, it tries to find for action '123' in news controller.

Basically all I want to do is that if user types /news/android , I want to capture 'android' to query and return the results. If there are too may results, need to support pagination i.e. url becomes /news/android/(2...n) .

Appreciate any help. thanks

+1  A: 

You can just do this:

Router::connect('/news/*', array('controller' => 'news', 'action' => 'onDemand'));

Have your onDemand function declared as:

public function onDemand($subject, $page = null)

When a user requests /news/android or /news/android/2 cake will call onDemand('android') or onDemand('android', '2'), respectively.

webbiedave
Thanks. I had that before but that breaks my another defined URL /news/recent/*. Sorry, I should have mentioned that in my original question.So basicallyif user type /news/recent/1 , it should go to action=indexif user types /news/<Anything else> - should point to action 'onDemand' I am playing around with the configuration right now. I am hoping that concept of precedence is there for routes.Thanks
aboxy
OK. I think this works. I have my route for /news/recent first which maps to action=indexand then /news/* after that. I can see both my links working. Any comments here?
aboxy
Interesting. That doesn't quite work for me, though. I had to have onDemand do `if ($subject == 'recent') $this->recent();` But if it works for you, great!
webbiedave
Hmmm, So far no issues. I ran through my test and everything seems to be active, no dead urls. Wonder if I am doing something wrong somewhere else. I really appreciate your help.
aboxy
No problem. Remember to officially accept the answer. Glad to help.
webbiedave