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

Redirect from controller

I have a two-stage form-filling process that I am trying to implement. Users enter data into a form, submit and are then taken to another page where they see the form data they just submitted and are presented with further fields to fill in.

Here is the relevant controller psuedo-code:

public class MyController extends Phalcon\Mvc\Controller{
    public function newAction($id){
        ...
        if($this->request->isPost()){
        if($form->isValid($this->request->getPost()) != false){
            $insert = new MyModel();
            $insert->assign(array( ...));
        }
        if(!insert->create()){
            //display errors
        }else{
            //insertion has succeeded
            $this->dispatcher->forward(array("controller"=>"MyController","acton"=>"edit","params"=>array($study->getPkStudy(),$saereport->getId())));
        }
}

The forward directive causes the page to submit like 10 times, which results in many insertions of $insert and eventually phalcon returns with a compaint about ifinitue loop. How can I redirect to another page after a post?

Dispatcher forwarding just does an internal redirect. That in one request from the brower, the initially requested action gets run (in this case my/new), then the action for my/edit gets run without the browser URL being updated.

Likely what you want is a redirect, which you can do like this:

$this->reponse->redirect('my/edit',[params array here]);

I'm not 100% sure about the second argument being parameters that get sent to the action - may want to do a little documentation research



40.7k

It looks like the syntax is supposed to be:

return $response->redirect(array(
    "for" => "index-lang",
    "lang" => "jp",
    "controller" => "index"
));

But I have no idea what those parameters mean except for controller. And there doesn't seem to be a place to pass runtime parameters :(



98.9k
edited Oct '14

What does "runtime parameters" mean?

Phalcon generally runs under the assumption you're running a RESTful application - which means all your parameters are included in the URL. So if for the sake of example I assume $study->getPkStudy() returns "apples", and $saereport->getId() returns "178", then you should be redirecting to:

my/edit/apples/178

You then set up a route that assigns "apples" and "178" to variables you can then access via the DI in your action.

Alternatively you can store those values in $_SESSION, or possibly include them in the URL like this:

my/edit?study=apples&id=178, though I'm not sure how redirect() handles GET variables like that.



40.7k

OK that makes sense, so what I am trying to redirect to would be

my/edit/apples/178

The question is how to use $this->response-redirect to tell Phalcon to go to that URL.



125.7k
Accepted
answer
$this->response->redirect('my/edit/apples/178');
$this->view->disable();

return ?

return $this->response->redirect('my/edit/apples/178');