Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How does Laravel handle PUT requests from browsers?

Tags:

laravel

I know browsers only support POST and GET requests, and Laravel supports PUT requests using the following code:

<?= Form::open('/path/', 'PUT'); ?>
    ... form stuff ...
<?= Form::close(); ?>

This produces the following HTML

<form method="POST" action="http://example.com/home/" accept-charset="UTF-8">
    <input type="hidden" name="_method" value="PUT" />
    ... form stuff ...
</form>

How does the framework handle this? Does it capture the POST request before deciding which route to send the request off to? Does it use ajax to send an actual PUT to the framework?

like image 751
Darrrrrren Avatar asked Feb 07 '13 17:02

Darrrrrren


People also ask

How does request work in laravel?

Laravel itself creates an instance of the application, is the initial/first step. Next step will occur on the Kernel part of the application. The incoming request is sent to either the HTTP kernel or the console kernel, depending on the type of request that is entering the application .

Which method allows you to verify that the incoming request path matches a given pattern in laravel?

The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method.

How do I create a request in laravel?

You can use replace() : $request = new \Illuminate\Http\Request(); $request->replace(['foo' => 'bar']); dd($request->foo); Alternatively, it would make more sense to create a Job for whatever is going on in your second controller and remove the ShouldQueue interface to make it run synchronously.

What method on a request instance provides access to an uploaded file called Foo?

laravel request has file regarding path method declares as well as returns the request's path erudition. The path method will return foo/bar: if the incoming request is targeted at http://domain.com/foo/bar.


2 Answers

It inserts a hidden field, and that field mentions it is a PUT or DELETE request

See here:

echo Form::open('user/profile', 'PUT');

results in:

<input type="hidden" name="_method" value="PUT">

Then it looks for _method when routing in the request.php core file (look for 'spoofing' in the code) - and if it detects it - will use that value to route to the correct restful controller.

It is still using "POST" to achieve this. There is no ajax used.

like image 198
Laurence Avatar answered Nov 11 '22 14:11

Laurence


Laravel uses the symfony Http Foundation which checks for this _method variable and changes the request to either PUT or DELETE based on its contents. Yes, this happens before routing takes place.

like image 14
William Cahill-Manley Avatar answered Nov 11 '22 15:11

William Cahill-Manley