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

Can I use \Phalcon\Mvc\Router\Annotations and \Phalcon\Mvc\Router at the same time?

I want to use the old way to define router. But with SEO needs, sometimes we have to change /foobar/baz -> /foo-bar/baz.

We always write another rewrite-rule to .htaccess to do this work. This is dangerous, 'cause it would affect the whole application. If I can add annotations for just that controller/action, and "/foo-bar/baz" can be routed to that controller/action, it will be perfect.

So the problem is: Is Router\Annotations an alternate or a complement?



98.9k
Accepted
answer

Router\Annotations is a variant of Router which loads the routes reading annotations from resources,

However, I think you could better implement a convertor in your route to remove the "-":

$di['router'] = function(){

    $router = new Phalcon\Mvc\Router(false);

    $router
        ->add('/:controller/:action', array(
            'controller' => 1,
            'action' => 2
        ))
        ->convert('controller', function($controller) {
            return str_replace('-', '', $controller);
        });

    $router
        ->add('/:controller/:action/:params', array(
            'controller' => 1,
            'action' => 2,
            'params' => 3
        ))
        ->convert('controller', function($controller) {
            return str_replace('-', '', $controller);
        });

    return $router;
};

Arrive at the same end by different means or roads. Thank you.