How does Laravel handle PUT requests from browsers?

后端 未结 6 1393
Happy的楠姐
Happy的楠姐 2020-12-20 12:17

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



        
相关标签:
6条回答
  • 2020-12-20 12:41

    in reality there is no PUT request ! its POST request ! when you tell laravel ; hey laravel this is a PUT request ! it means you want to update some resource ! {{Form::open('user/profile', 'PUT')}} the above line means its PUT request (just it means updating a resource as I mentioned before there is no PUT request ) that line of code create a hidden filed in form that specify the type of request and when laravel router get the request it search for PUT handler of the URI for example sth like : Route::put('the/url/you/mention/here/' , 'someController@someAction');

    0 讨论(0)
  • 2020-12-20 12:45

    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.

    0 讨论(0)
  • 2020-12-20 12:54

    You can also use an array within your form open like so:

    {{ Form::open( array('route' => array('equipment.update', $item->id ),
    'role' => 'form',
    'method' => 'put')) }}
    

    Simply change the method to what you want.

    0 讨论(0)
  • 2020-12-20 12:55

    PUT usually refers to update request.

    When you open a form inside laravel blade template using,

    {{ Form::open('/path/', 'PUT') }}
    

    It would create a hidden field inside the form as follows,

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

    In order for you to process the PUT request inside your controller, you would need to create a method with a put prefix,

    for example, putMethodName()
    

    so if you specify,

    {{ Form::open('controller/methodName/', 'PUT') }}
    

    inside Form:open. Then you would need to create a controller method as follows,

    class Controller extends BaseController {
        public function putMethodName()
        {
            // put - usual update code logic goes here
        }
    }
    
    0 讨论(0)
  • 2020-12-20 12:57

    While a late answer, I feel it is important to add this for anyone else who finds this and can't get their API to work.

    When using Laravel's resource routes like this:

    Route::resource('myRoute','MyController');
    

    It will expect a PUT in order to call the update() method. For this to work normally (outside of a form submission), you need to make sure you pass the ContentType as x-www-form-urlencoded. This is default for forms, but making requests with cURL or using a tool like Postman will not work unless you set this.

    0 讨论(0)
  • 2020-12-20 12:59

    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.

    0 讨论(0)
提交回复
热议问题