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: http://phalcon.local/MyProject/admin/login but I recived url like this: http://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?



84.0k

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

Yes, in case if present route I want get alias, for example: ```php $router->add('/admin/login', [ 'module' => 'backend', 'namespace' => 'MyProject\Backend\Controllers\', 'controller' => 'index', 'action' => 'login' ]); ``` So if i get to linkTo: ```php Phalcon\Tag::linkTo('backend/index/login'); ``` I want recive in result url: ```php admin/login ``` as in route rule.



84.0k

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"]);

http://docs.phalconphp.com/en/latest/reference/url.html#generating-uris http://docs.phalconphp.com/en/latest/reference/micro.html#generating-urls-for-routes

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