Laravel Redirect as POST - laravel

Currently my users must get the visit form given by Route::get then fill it in to get back a result view given by Route::post. I need to create a shareable link such as /account/search/vrm/{vrm} where {vrm} is the VRM that is usually filled in on the form page. This VRM then needs to redirected to Route::post as post data. This needs to be done by my controller. How can I do this in my controller?
Routes:
// Shows form view
Route::get('/account/search', 'User\AccountController#getSearch')->name('account.search');
// Shows result view
Route::post('/account/search', 'User\AccountController#runSearch');
// Redirect to /account/search as POST
Route::get('/account/search/vrm/{vrm}', function($vrm) { ???????? });

POSTs cannot be redirected.
Your best bet is to have them land on a page that contains a form with <input type="hidden"> fields and some JavaScript that immediately re-submits it to the desired destination.

You can redirect to a controller action or call the controller directly, see the answer here:
In summary, setting the request method in the controller, or calling a controller's action.
Ps: I don't want to repeat the same thing.

For those who comes later:
If you are using blade templating engine for the views, you can add '#csrf' blade directive after the form starting tag to prevent this. This is done by laravel to prevent cross site reference attacks. By adding this directive, you can get around this.

return redirect()->route('YOUR_ROUTE',['PARAM'=>'VARIABLE'])

Related

Is there a way to get the page url a form was submitted from without to hide it (url) to the form?

I have different pages where users can add comments. I want to save the url of the page where the comment was made so that I can see it afterwards. Is this possible without hiding the the current url to the comment form? For example has the request() such a property? Or is hiding the url to the form the only way for that.
Yes there is a global helper for that
url()->previous();
url()->current();
url()->full();
If you prefer to use $request:
$request->url();
https://laravel.com/docs/5.4/helpers#urls

Check to see if a page is called via Ajax in Laravel

This is an extension from a post I made a few days ago: Change a page without refreshing - Laravel / Ajax
Basically, I'm trying to replicate the URL Structure of Soundcloud where you can click on a link, it'll load the content without refreshing the page however if you land directly on that page, it won't replicate design and effecitvely break.
I've been thinking of ways on how I can check in Laravel if the page is requested via Ajax or has been landed on without an Ajax call.
What's happening at the moment is that when I call the page, the view has a master template that's extended thus creating duplicate master templates on the one view.
I was thinking if I done something like
#if(!Request::ajax())
#extends('masterlayout')
#endif
It would work but tried and no luck.
Any help as always is greatly appreciated!
Thanks
Looks like #extends directive is always executed even in falsy if
You can add an empty layout and perform check like this:
#extends(Request::ajax()? 'layouts.empty' : 'layouts.master')
and you need add only this in layout empty.blade.php:
#yield('content')
or you can add ajax check in the master layout
#if(!Request::ajax())
// all layout code
#else
#yield('content')
#endif
You can check that in your controller action.
public function index(Request $request)
{
if($request->ajax()){
return "This is an AJAX call!";
}
return "Not an AJAX call...";
}
Thanks for all the answers, I eventually got this fixed by using #if(!Request::ajax()) on section that didn't need to be shown when the page was loaded via ajax.
Thank you again! :D

in laravel view I want the content of the url and not the url itself

I would like to show the content instead of the url in my view. How can I use Laravel to make this work?
At the moment, in my Laravel view I have:
Link
<p>{!! route('plaats.text', ['plaats' => $plaatsje]) !!}</p>
<p>{!! action('TablesController#text', ['plaats' => $plaatsje]) !!}</p>
The first creates a link with a working active url, the second and third a death link: "http://mysecondsite.dev/koop/text/plaats/capelle-aan-den-ijssel".
Following the link itself (in all cases) returns the expected content of the url (So, my routing seems to be ok). But neither returns the underlying content.
regards
Peter
I think what you are looking for is blade service injection
It's bad practice to inject logic into views.
What you probably should do is do the operations before the view, and then pass the results (contents) to the view and echo them

Controller redirect back to a POST form

I have a table showing a list of names, with an "edit" button and hidden id value at the side.
Clicking the "edit" button will post the hidden id as a form value and display the edit page so the user can change details of that person. Pretty standard.
When editing the details and submitting I'm using a validator. If the validation fails it needs to go back to the edit page and display the errors. Problem is that the edit page required an Id value via POST method, but the redirect only seems to utilise the GET method which leads to a "Controller Method Not Found" error as there is no Get route set up.
Does anyone know how I can redirect back to the page via POST not GET. Currently my code looks like:
public function postEditsave(){
...
if ($validator->fails())
{
return Redirect::to('admin/baserate/edit')
->withErrors($validator)
->withInput();
}else{
...
thanks
You can use Redirect::back()->withInput();
You may wish to redirect the user to their previous location, for example, after a form submission. You can do so by using the back method
See: http://laravel.com/docs/5.0/responses
You don't need to use POST to go to the edit page. You can use GET and a parameter to the route, check this out: http://laravel.com/docs/routing#route-parameters
You'd have a GET route to show the edit page, and a POST route to handle the request when the user submits the form.
It'll look like this (notice the parameters):
public function getEdit($id)
{
return View::make(....);
}
public function postEdit($id)
{
...
return Redirect::back()->withErrors($validator)->withInput();
}
if "redirect with POST" is exist, then I don't know it. I recomend you to just use flash data
Redirect::to('user/login')->with('id', 'something');
You can use Redirect::to("dashboard/user/$id")->withErrors($validator)->withInput();. You should use double quote to pass parameter if there is any errors with validation.

RedirectToAction MVC 3 after $.ajax call

From my view I am sending via $.ajax a JSON object to my controller to save it in the database.
If all succeeded i want to redirect to another action which will show a diferent view.
If i use this code:
return RedirectToAction("CreatePage", "Survey", new {id = question.PageId});
The execution goes to the Survey controller which returns a view but it is not shown.
I have read some post which said that it is not posible to redirect via ajax.
The solution I use so far is to redirect via javascript like this:
success: function (ret) {
window.location.href = "/Survey/CreatePage/" + $("#PageId").val();
}
Although this always works, sometimes i need to refresh the CreatePage view to show the last changes made.
Any idea of how to solve this problem better?
Thanks in advance
As mccow002 suggested, I wasn't really needing to make the call via AJAX for that part. After studying the solutions suggested, i realized that i could simple submit it in a form. My confusion came because I have a save and continue editing and a save. For the save and continue I use the AJAX call, but for the save option with the form being submitted is ok.
Thanks very much for your help.
Instead of redirecting to a new page, you can send a rendered html from .net code back to client and load that html in page, like this $("#main").load(renderedHtml).
But for refreshing the page you can write a simple script that run at specified intervals and refresh the page contens.
You could use [OutputCache] on the CreatePage action so that it doesn't cache the page or only caches for so long.
output caching

Resources