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

Phalcon route conflict

I have a problem with routes on phalcon. I have code:

$router->add('/{aliascat:[0-9\-a-z\-]+}(/?)', array(
'module' => 'frontend',
'controller' => 'category',
'action' => 'view',
'category' => 1,
'lang' => 'md',
))->setName('category_view_cpu');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{aliascat:[0-9\-a-z\-]+}(/?)', array(
'module' => 'frontend',
'controller' => 'category',
'action' => 'view',
'category' => 2,
'lang' => 1,
))->setName('category_view_cpu');

$router->add('/{controller:[a-z]{3,50}+}(/?)', array(
'module' => 'frontend',
'controller' => 1,
'action' => 'index',
'lang' => 'md',
))->setName('default_module');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}(/?)', array(
'module' => 'frontend',
'controller' => 2,
'action' => 'index',
'lang' => 1,
))->setName('default_module_lang');

$router->add('/{controller:[a-z]{3,50}+}/:action/', array(
'module' => 'frontend',
'controller' => 1,
'action' => 2,
'lang' => 'md',
))->setName('default_module_controller_action');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}/:action/', array(
'module' => 'frontend',
'controller' => 2,
'action' => 3,
'lang' => 1,
))->setName('default_module_controller_action_lang');

$router->add('/{controller:[a-z]{3,50}+}/:action/:params/', array(
'module' => 'frontend',
'controller' => 1,
'action' => 2,
'params' => 3,
'lang' => 'md',
))->setName('default_module_controller_action_params');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{controller:[a-z]{3,50}+}/:action/:params/', array(
'module' => 'frontend',
'controller' => 2,
'action' => 3,
'params' => 4,
'lang' => 1,
))->setName('default_module_controller_action_params_lang');

I get undifined controller for category route, because conflict with default_module route. How I can fix this problem?

I have route: /category-name/ And default: /controller/action/...

Conflict with routes.

Hi @Dronax you have to create a router with first parameter in false to prevent the default router

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

Good luck



1.5k

But Where I can add this code? In Controller?

Hi @Dronax you have to create a router with first parameter in false to prevent the default router

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

Good luck



1.5k
edited Dec '17

I have this in routes, not working:

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

$router->setUriSource(\Phalcon\Mvc\Router::URI_SOURCE_SERVER_REQUEST_URI);
$langsDefined = 'md|ru|en';

$router->add('/{catalias}(/?)', array(
    'module' => 'frontend',
    'controller' => 'category',
    'action' => 'view',
    'aliascat' => 1,
    'lang' => 'md',
))->setName('cat_view_short_ee');

$router->add('/{lang:[' . $langsDefined . ']{2}+}/{catalias}(/?)', array(
    'module' => 'frontend',
    'controller' => 'category',
    'action' => 'view',
    'aliascat' => 2,
    'lang' => 1,
))->setName('cat_view_short_lang_ee');

$router->setDefaultModule('frontend');
$router->setDefaultController('index');
$router->setDefaultAction('index');

Hi @Dronax you have to create a router with first parameter in false to prevent the default router

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

Good luck