We have moved our forum to GitHub Discussions. For questions about Phalcon v3/v4/v5 you can visit here and for Phalcon v6 here.

multiple routes pattern problem

Hi I've got problem with multiple route patterns, to handle different routes by one controller action.

        https://www.blabla.com/search/aa/blabla/

        https://www.blabla.com/search/aa/blabla/?from=2015-11-12&to=2015-11-16

        https://www.blabla.com/search/aa/blabla.php?from=2015-11-12&to=2015-11-16

        https://www.blabla.com/search/aa/blabla/2015-11-12/2015-11-16/

        https://www.blabla.com/search/aa/blabla/from/2015-11-12/to/2015-11-16/       

my routing

                    $router->add('/search/{a:[a-zA-Z]{2}}/{b:[a-zA-Z0-9_-]+}/:params',
                        array(
                            "controller" => "search",
                            "action"     => "index",
                            'params' => 3
                        )
                    );

                    $router->add('/search/{a:[a-zA-Z]{2}}/{b:[a-zA-Z0-9_-]+}/{from:/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/}/{to:/^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/}/:params',
                        array(
                            "controller" => "search",
                            "action"     => "index",
                            'params' => 5
                        )
                    );

when I run https://www.blabla.com/search/aa/blabla/ it gives me Action aa was not found on handler search

$this->dispatcher->getParam("from"); not working either



9.8k
Accepted
answer
edited Sep '15

Not sure but i tested your code and it didn't work until i moved it at the end of routing list. So i guess it has something to do with what the documentation on routing states: https://docs.phalcon.io/pl/latest/reference/routing.html :

Internally, all defined routes are traversed in reverse order until Phalcon\Mvc\Router finds the one that matches the given URI and processes it, while ignoring the rest.

The bold highliting for the "reverse" word is mine of course.



8.4k

for my second problem $this->dispatcher->getParam("from"); it seems there is a problem on my regexpression, and i dont know why

        /^[0-9]{4}-(0[1-9]|1[0-2])-(0[1-9]|[1-2][0-9]|3[0-1])$/
        2015-01-01
        2015-12-31

anyhow i changed it to simple one