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

URL generation issue with Prefixed Router Group

Hi everyone,

I have a little problem with redirection and prefixed routerGroup. I'm using Phalcon 3.2.1.

An example is better than a laïus.

// [...]

$router = new Router(false);

// Create a group with a common module and controller
$backendRouter = new RouterGroup(
    [
        "module" => "backend",
    ]
);
// Add admin directory to all backend URL
$backendRouter->setPrefix('/my-own-admin-dir');

$backendRouter->add('/',
    [
        "controller" => "index",
        "action"     => "index",
    ]
)->setName('backend-index');

$backendRouter->add('/login',
    [
        "controller" => "login",
        "action"     => "index",
    ]
)->setName('backend-login');

$router->mount($backendRouter);

// [...]

And inside a security plugin (like invo example)

// Those lines are just for debuging
$route = $this->router->getRouteByName('backend-login');

print_r($route->getReversedPaths()); // => Array ( [backend] => module [login] => controller [index] => action )

echo $route->getPattern().'<br>'; // => /my-own-admin-dir/login

echo $this->url->get($route->getPattern()).'<br>'; // => /my-own-admin-dir/my-own-admin-dir/login

echo $this->url->get(array(
    'for' => 'backend-login'
)).'<br>'; // => /my-own-admin-dirmy-own-admin-dir/login

// This is my original code which redirect to "/my-own-admin-dirmy-own-admin-dir/login"
return $this->response->redirect(array(
    'for' => 'backend-login'
));

I've added results to echo command in code comments. As you can see, prefix is repeated. I've tested several configuration with and without the trailing slash and also with "$router->removeExtraSlashes(true);", but it still does not work :(

Another strange thing, when I write the same debugging code in 'config/services.php' file, there is no problem at all ! (no double prefix).

Thanks for your help

Don't you have maybe something like base url or something?



607
Accepted
answer

Thanks a lot Wojciech.

I haven't figured out that BaseUri could be the cause.

And my error was to redefine the BaseUri in the backend module, WITH the backend prefix :(