Solved thread

This post is marked as solved. If you think the information contained on this thread must be part of the official documentation, please contribute submitting a pull request to its repository.

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?



83.4k
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;
};


4.6k

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