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 to do redirect back/previous in Phalcon?

Hello!

Tldr: How to redirect to previous view? Or only call a controller and update database without effecting view.

I have a view which produces a follow button. Quite simply:

{% if user.id !== auth_user.id %}
    {% if user.isFollowedBy(user) %}
        {{ link_to("friends/new/" ~ user.id, '<i class="glyphicon glyphicon-edit"></i> Follow', "class": "btn btn-success") }}
    {% else %}
        {{ link_to("friends/delete/" ~ user.id, '<i class="glyphicon glyphicon-edit"></i> Unfollow', "class": "btn btn-primary") }}
    {% endif %}
{% endif %}

This button appears on a lot of different views in my site. For example on a status or someones profile. Therefore I want it to redirect to the previous page where it came from. For example if you clicked follow on somones profile, it should return to their profile (Basically call the controller perform database changes and nothing else).

In laravel I used:

return Redirect()->back();

But I can't get something similar to work in Phalcon, only defined paths by controller and action.

Here is my controller:

    public function newAction($id){
        $returnUser = Users::findFirstById($id);
        if($returnUser){
            $userID = $this->user->id;
            $friend = Friends::findDeleted(
                "user_id = " . $userID . " AND followed_id = " . $id
                );
            if(!$friend){
                $friend = new Friends();
                $friend->user_id = $userID;
                $friend->followed_id = $id;
                $friend->deleted = 0;
            }
            else{
                $friend->deleted = 0;
            }
            if (!$friend->save()) {
                foreach ($friend->getMessages() as $message) {
                    $this->flash->error($message);
                }
            }
            else {
                $this->flash->success("You are now following " . $returnUser->username);
                return $this->forward('profile/show/' . $returnUser->username);
                //return $this->response->redirectBack();
            }
        }
        else{
            $this->flash->error("User by that id does not exist.");
            return $this->forward('profile/show/');   
        }
    }

In every redirect I want the previous, not a defined path which I'm currently using. Since if you call the controller from a status, I want to return to that status and not their profile.

Thank you!!



3.0k
Accepted
answer

I use the following in my base controller:

     /**
 * Go Back from whence you came
 * @return type
 */
protected function _redirectBack() {
    return $this->response->redirect($_SERVER['HTTP_REFERER']);
}

So in your case wherever you want to go back to previous action just use return $this->_redirectBack()

Works like a charm. Thank you! :)

I use the following in my base controller:

    /**
* Go Back from whence you came
* @return type
*/

protected function _redirectBack() { return $this->response->redirect($_SERVER['HTTP_REFERER']); }

So in your case wherever you want to go back to previous action just use return $this->_redirectBack()

edited Jun '15

Even better!

edited Oct '15

Also there is a built in method in phalcon request, which you can use:

return $this->response->redirect($this->request->getHTTPReferer());


431

is it possible to dispatch->forward to a previous action?