Router setName different since update to 2.0

Hi all,

I've install last Phalcon version 2.0.0 and I found a difference with routing when I use the same route Name.

My Router :

$this->di->set('router', function ()
        {        
            $router = new \Phalcon\Mvc\Router(false);        
            $router->setDefaultModule("main");                    
            $router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);

            $router->notFound(array('controller' => 'error', 'action' => "error404" ));        
            $router->removeExtraSlashes(true);        
            require (APPLICATION_PATH . '/config/routes.php');        
            return $router;
        });

File : routes.php (an exemple)

$router->add('/post', [ 'module' => 'main', 'controller' => 'post', 'action' => 'index'])->setName('mainPostIndex')->via(array('GET'));
$router->add('/post/{id:[0-9]+}', [ 'module' => 'main', 'controller' => 'post', 'action' => 'index'])->setName('mainPostIndex')->via(array('GET'));

Link Example in Volt with URL Component :

<a href="{{ url(['for': 'mainPostIndex' ]) }}"> Foo </a>
<a href="{{ url(['for': 'mainPostIndex', 'id' : 1 ]) }}"> Foo </a>

Phalcon 1.3.4 : both routes are matched and work perfectly.

url = /post   or /post/ 
url = /post/1

Phalcon 2.0 : Only first route work. The second is overwrited

url = /post
url = /post

An Idea ?

Thx.



4.2k

You shouldn't need to define the route twice. Since they're both going to the same place, you should be able to define the following

// Make the parameter optional
$router->add('/post/{id:[0-9]*}', [ 'module' => 'main', 'controller' => 'post', 'action' => 'index'])->setName('mainPostIndex')->via(array('GET'));

What you'll get from the dispatcher is an empty ID when just going to /post but a valid id when you pass one.



3.2k

I try optional parameter like your example but it the same or doesn't work :(

I also try :

(optional slashe)

$router->add('/post[/]{id:[0-9]*}', [ 'module' => 'main', 'controller' => 'post', 'action' => 'index'])->setName('mainPostIndex')->via(array('GET'));
$router->add('/post/{id:[0-9]+}*', [ 'module' => 'main', 'controller' => 'post', 'action' => 'index'])->setName('mainPostIndex')->via(array('GET'));

So I just set a new name for my route.