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

Parameters in Router Annotations

Hi,

I have a problem with parameters in \Phalcon\Mvc\Router\Annotations. I want change my long routing file to annotations mechanism which is awesome. Before I have something like this:

$router->add('/example/all/(\d{4})/(\d{2})([/]?)', array(
    'controller' => 'example',
    'action' => 'index',
    'year' => 1,
    'month' => 2
))->setName('example-name');

I change this rule to annotation in indexAction of ExampleController:

    /**
     * @Route("/all/(year:\d{4})/(month:\d{2})", name="example-name")
     */
    public function indexAction($year = null, $month = null)
    {
        if (is_null($year) || is_null($month)) {
            $year = date('Y');
            $month = date('m');
        }

        ...

        $this->view->year = $year;
        $this->view->month = $month;
    }

In view I use named route:

{{ url(['for': 'example-name', 'year': year, 'month': month]) }}

Before I got this url: https://mydomain.com/example/all/2014/07/

After change to annotations I got: https://mydomain.com/example/all//

@Phalcon is it a bug or am I doing something wrong?

//cc @Baryshev @Dreamsxin



5.2k
Accepted
answer

My fault, I used round brackets () instead of braces {} for defining parameters. The annotation should be looks:

    /**
     * @Route("/all/{year:\d{4}}/{month:\d{2}}", name="example-name")
     */