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

Router and baseUri

Hi everyone!

Here is a new question about router and baseUri application. I set a base uri to /admin/ for my backend module.

If i use :

$url->get("products/save")

or

$this->url->get(array( 'for' => 'myRouteName', 'controller' => 'product', 'action' => 'save' ));

It will add a double /admin/admin/ which is normal because my route is define this way :

$router->add( '/admin/:controller/:action', array( 'module' => 'backend', 'namespace' => 'App\Backend\Controllers\', 'controller' => 1,
'action' => 2, ) )->setName('myRouteName');

The thing is if i remove /admin in my route, it won't still work anymore :)

I guess it's a stupid question but can't find the right solution!



4.7k

Try to use RouterGroups

//Create a group with a common module and controller
$admin = new RouterGroup(array(
    'controller' => 'index'
));

//All the routes start with /admin
$admin->setPrefix('/admin');

// now simply add your route
$admin->add( '/:controller/:action', array(
    'module' => 'backend',
    'namespace' => 'App\Backend\Controllers',
    'controller' => 1,
    'action' => 2
    )
)->setName('myRouteName');


8.2k

Seems to be a good solution, only thing is my routes file contains both frontend and backend routes. Maybe i might be divided my routes in two distinct files.