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.

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?



166
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 :(