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!!