SaidbakR
SaidbakR

Reputation: 13534

Phalcon PHP dispatcher forward does not work

I'm following this official tutorial to learn framework version 2.0.9.

I have a voteAction of the PollController the following code supposed to redirect or forward to showAction after increment number_votes as follows:

public function voteAction($optionId)
{
    $option = PollOptions::findFirstById($optionId);
    $option->number_votes++;
    $option->save();
    return $this->dispatcher->forward([                
        'action' => 'show',
        'params' => [$option->poll_id]
    ]);
}

However, after voting the I have got poll/vote/xx not poll/show/yy, where xx is the option id and yy is the poll is respectively.

I have tried to add 'controller' => 'poll' and replacing [] array definition by array() but nothing. However, when I use invalid or not found action name for the key action such as showAction or blah it returns the following error:

Action 'showAcyoimn' was not found on handler 'poll'
#0 [internal function]: Phalcon\Mvc\Dispatcher->_throwDispatchException('Action 'showAcy...', 5)
#1 [internal function]: Phalcon\Dispatcher->dispatch()
#2 C:\xampp\htdocs\blog\public\index.php(29): Phalcon\Mvc\Application->handle()
#3 {main}

Upvotes: 2

Views: 2217

Answers (1)

Nikolay Mihaylov
Nikolay Mihaylov

Reputation: 3866

Forward does not make HTTP redirect. More info here: https://docs.phalconphp.com/en/latest/reference/dispatching.html#forwarding-to-other-actions

If you want to redirect to other url use:

return $this->response->redirect(array(
  "controller" => "index",
  "action" => "actionName",
  "params" => "paramValues"
));

For example Forward is useful to show 404 page.

Upvotes: 6

Related Questions