PHP after sending response send another link - laravel

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');

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.4 post route returns no response

I can't get even a simple a Laravel post route to return anything. If I switch to get it works fine. Chrome network tab shows the request is a 200 code, but the response is empty even if I dd() something.
Route::post("/login", function(){
dd("test");
});
Also the request is definitely reaching the route correctly, as I can write a file (file_put_contents), but I can't get any return from the post route, whether is a return response()->json(), dd(), var_dump, or just echo.

laravel Response:download()->with() not working

return response()->download($resultPath."/".$csvfilename,$csvfilename, $headers)->with("success","success"); not working
how to pass parameter in return in laravel
Maybe you should be able to do return response()->with('success', 'success')->download(...) but I'm not sure.
If you really need to check the success, try to set a header or to check the HTTP status code.

How to correctly redirect after doing POST in 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);

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