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

How I can simply generate a link using the controller/actions/params?

I am trying to utilize the Tag::linkTo functionality in my views to generate links. However, I do not want to explicitly type a link (such as /my-url).

For example, I have a controller called GameController. Default routes are enabled, so this is accessible via /game/ and /game/index, as you would expect. I have created a custom route allowing this to also be accessible via /overview.

I want to pass the controller and the action I wish to link to, and then generate a link to whatever route was matched. In this scenario, I would pass it controller => game, action => index and it would return /overview - falling back to default routing (like /game/ or /game/index) if no route was defined for that pair. This would allow my websites links to update automatically if the route to a particular controller/action changes.

Is there some way to do this? In CakePHP, the HTML helper can accept controller/action arguments and return a link to the appropriate route. I am not seeing similar functionality in any of the linkTo examples on the documentation page.

edited Dec '14

Your routes doesn't know if game exists or not. Just check game by ID or name inside action and if not exists in DB -> redirect to overview.

$router->add('/game/{id:[0-9]+}', {

'controller' => 'game',

'action' => 'index'

});

Inside index action:

$id = $this->dispatcher->getParam('id');

$game = Games::findFirst($id);

if ( ! $game)

return $this->response->redirect('overview');

https://docs.phalcon.io/en/latest/reference/dispatching.html#getting-parameters



12.6k
edited Dec '14

Your routes doesn't know if game exists or not. Just check game by ID or name inside action and if not exists in DB -> redirect to overview.

$router->add('/game/{id:[0-9]+}', {

'controller' => 'game',

'action' => 'index'

});

Inside index action:

$id = $this->dispatcher->getParam('id');

$game = Games::findFirst($id);

if ( ! $game)

return $this->response->redirect('overview');

https://docs.phalcon.io/en/latest/reference/dispatching.html#getting-parameters

Hi. Thanks for your response but I believe you misunderstood my question.

By default, the route /game/index points to GameController::indexAction. I have added a route - /overview - that also points to the same action.

I want to use something like linkTo, but I want it to utilize the route I have specifically set for that controller and action, not the default controller/action url.

There is no issue at all involving models, or "games", or game ids. I simply want to generate a link to controller actions using a route I explicitly created, if it exists, and ten fall back onto the default route if needed.

edited Aug '18

Hello! I'm not so good at this, because I develop only jackpot city review websites. But here is a link with great guide (https://guides.rubyonrails.org/v5.1/action_controller_overview.html). I think It can be useful for you.