How to correctly redirect after doing POST in Laravel? - laravel

I have multiple routes with comments and when I click reply I get redirected to a route where I can post a reply to a comment. How can I correctly store the route from where I came and then redirect back to it after posting the reply?
I considered passing the URL::previous as a param and storing it into a hidden input, but if the page gets refreshed by the user it gets empty. Another way might be store in the session, but then I don't know how to reliably expire it...

Redirect back with success message
return redirect()->back()->with('success', 'Data added successfully');

use return redirect()->back();

Use return Redirect::back() function for the previous URL.

You can use return Redirect::back(); or return Redirect::to(URL::previous() . "#whatever");

You can keep 2-3-4-5 URLs in a session and you don't need to expire it. You can just limit number of kept URLs. Also, please check my answer to a similar question here.

Store url in the session when user access page, when you have your reply button. You dont have to expire it, it will get automaticaly updated when user visit next page with reply button.
session(['last_url' => 'Request::fullUrl()']);
Also dont forget to use namespace
Use Request;
If you really want to discard value from session after user redirects, you can use this:
return redirect()->url(Session::pull('last_url'));
And namespace
Use Session;

Simply you can do return redirect()->back();

Do like this,
First store url in session
$request->session()->put('previous-url', '/user/demo');
Use like this
$previous_url = Session::get('previous-url');
return redirect()->to($previous_url);

Related

Laravel Redirect and Change URL

I thought this would be an easy task and would be the default functionality ... I was wrong.
I am attempting to redirect the user to the dashboard once logged in, I have tried this several ways:
return redirect('/dashboard');
return redirect()->to('/dashboard');
return redirect()->route('dashboard');
All of these work to display the dashboard, but they do not change the URL, so you cannot tell the user location from the address. Could someone please tell me what is required to achieve this.
i had this problem & saw that my mistake was in the Route file.
i had twice a time [..., 'index'] action defined in the same controller
Route::post('/cart', [CheckoutController::class, 'index'])->name('checkout.cart');
Route::get('/checkout', [CheckoutController::class, 'index'])->name('checkout.index');

Laravel 5.7 Passing a value to a route in a controller

My controller posts a form to create a new page. After posting the form I need to redirect the user to the new page that will have the contents for that page that were entered in the previous form. If I simply do return view('mynewpageview', compact('mycontent')); where my mycontent is the object used to execute the $mycontent->save(); command, I carry the risk for someone refreshing the url thus posting the same content twice by creating a new page.
Instead I would like to redirect the user to the actual page url.
My route is
Route::get('/newpage/{id}', 'PageController#pagebyid'); and if I use return redirect()->route('/newpage/$pageid'); where $pageid = $mycontent->id; I get Route not defined error.
What would be the solution either to stop someone from resubmitting the content or a correct syntax for passing the parameter?
The correct answer that works for me is -
Give your route a name in the routes file
Then pass the parameters with an array as shown below in the controller.
return redirect()->route('newpageid', ['id' => $pageid]);
With basic (unnamed) routes, the correct syntax was return redirect('/newpage/'.$pageid);
You have already found out you can alternatively use named routes.
Last but not least, thanks for having considered the "double submit" issue! You have actually implemented the PRG pattern :)

How to return to intended page in this case

I have the typical scenario of an online newspaper where you have the article and below a form to comment, but where you need to login to comment. So on clicking it takes you to the log page and it should return you to that page once you have authenticated.
So, because the form is part of a page that the user can see without being logged in, I cannot write the middleware for that page created by a PostController, (get route).
The CommentController only has one method, which is the store one for the Form. so, of course, if I placed a middleware for that controller, it would fail because although it would indeed take you to the login page on clicking the submit button, the Intended URL saved would be that Post for the form, so on returning after authenticating, it would take you to a non existent URL page under the name of the Post route for that Form.
I have read about Auth::guards and the like but could not come clear with it,
This
Laravel 5 - After login redirect back to previous page
asks exactly the same question that I do, it is the same scenario, but I dont see how his answer works, because defining a protected variable (and I have that already) like protected $redirectTo in the Auth controller only tells where to go after authenticating, and it is only a fixed route, in my case it takes you to the dashboard. But I dont want to be taken to the dashboard, it has to return to the page where the article and the comment form are.
I found the solution at Laracasts. I must say I really dont understand it, but it works. It adds two functions to the AuthController.
https://laracasts.com/discuss/channels/laravel/redirect-to-page-where-login-button-was-clicked
public function showLoginForm()
{
if(!session()->has('from')){
session()->put('from', url()->previous());
}
return view('auth.login');
}
public function authenticated($request,$user)
{
return redirect(session()->pull('from',$this->redirectTo));
}
It's been a while since i've done this, but this should work normally.
You could add a GET param in your link from the comment section to login page.
http://....com/login?intended=http://yourredirectlink.com/#form
Put the intended url in in the session variable url.intended and after login you redirect like so
Redirect::intended('/');
This will redirect back to the the url '/' if the session variable is not available.

PHP after sending response send another link

one of my system needs to implement such a ways, that after getting on my link i replied a
return response('OK', 200);
or
return response('Unauthorized', 401);
Now, i need to implement such a ways that after returning ok response, i need to go through another link. How am i supposed to implement that? I am using php laravel. I am kind of unsure how these will work. Will i get to go to another laravel route for that or any other ways?
Thanks.
Try:
return response()->setStatusCode(200, 'OK');
and
return response()->setStatusCode(401, 'Unauthorized');
For redirection you can use redirect() function.Or if response ok then you can go for redirect another route what you want.
return redirect('user/login')->with('message', 'Login Failed');

Laravel - Path of page from which request was made

How can I determine the path of the page from which the request was made?
I have tried $request->path() or Request::path() but these two return the path on which request is going..
You may try this:
$request->header('referer');
Also URL::previous will give you that URL, for example:
\URL::previous();
Update: You may use this to send a user back to form on failed validation:
return redirect()->back(); // You may use ->withInput()->withErrors(...) as well

Resources