André Luiz André Luiz - 4 months ago 15
PHP Question

Phalcon PhP - how to use named routes inside a controller

I'm having trouble finding how to get the Urls from named routes inside a Phalcon PhP controller. This is my route:

$router->add(
'/admin/application/{formUrl:[A-Za-z0-9\-]+}/{id:[A-Za-z0-9\-]+}/detail',
[
'controller' => 'AdminApplication',
'action' => 'detail'
]
)->setName("application-details");


I want to get just the Url, example: domain.com/admin/application/form-test/10/detail . With the code below I can get the html to create a link, the same result of the link_to.

$url = $this->tag->linkTo(
array(
array(
'for' => 'application-details',
'formUrl' => $form->url,
'id' => $id
),
'Show'
)
);


The result I want is just the Url. I'm inside a controller action. I know it must be really simple, I just can't find an example. Can you help me?

Thanks for any help!

Answer

You should use the URL helper. Example:

$url = $this->url->get(
    [
        'for' => 'application-details', 
        'formUrl' => $form->url,
        'id' => $id,
    ], 
    [
        'q' => 'test1',
        'qq' => 'test2',
    ]
);

You can pass second array for query string params if needed.

According to your route definition, the above should output something like:

/admin/application/form-url/25/detail?q=test1&qq=test2

More info of Generating URIs in the docs.

Comments