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

Problem with User-friendly URLs

Hello Guys, I want to get User-friendly URLs. I set route, for example:

    $router->add('/admin/login', [
        'module' => 'backend',
        'namespace' => 'MyProject\Backend\Controllers\\',
        'controller' => 'index',
        'action' => 'login'
    ]);

Then I create link:

  Phalcon\Tag::linkTo($module.'/'.$controller.'/'.$action, $option['caption']);

in case if module is 'backend', controller is 'index' and action is 'login' i hoped get link like this: https://phalcon.local/MyProject/admin/login but I recived url like this: https://phalcon.local/MyProject/backend/index/login I try using

  $url->get($module.'/'.$controller.'/'.$action); 

unfortunately i get MyProject/backend/index/login again. Can you help, How to solve this problem?



98.9k

Do you want to generate a URI based on a route?



1.2k

Yes, in case if present route I want get alias, for example:

$router->add('/admin/login', [
        'module' => 'backend',
        'namespace' => 'MyProject\Backend\Controllers\\',
        'controller' => 'index',
        'action' => 'login'
    ]);

So if i get to linkTo:

Phalcon\Tag::linkTo('backend/index/login');

I want recive in result url:

admin/login 

as in route rule.



98.9k

You need to name your routes to generate URIs from them:

$router->add('/admin/login', [
        'module' => 'backend',
        'namespace' => 'MyProject\Backend\Controllers',
        'controller' => 'index',
        'action' => 'login'
])->setName('backend-index');
echo Phalcon\Tag::linkTo([['for' => 'backend-index'], "A link to the backend's index"]);

https://docs.phalcon.io/en/latest/reference/url.html#generating-uris https://docs.phalcon.io/en/latest/reference/micro.html#generating-urls-for-routes



1.2k

So, this converting imposible just in case: 1) handle set route name; 2) set parameter for in link; Thanks a lot.