How To Get Current Request Method Post Patch Get In Laravel - PHP

  • 28-06-2022
  • 942
  • Laravel 9
  • Haresh Chauhan

Sometimes developers used multiple methods in a single route and they want different actions with different methods. So they need to identify what request method coming into sever and then after identifying they will action accordingly.

The benefit is you can reduce your routes and utilize Maximus's use of single route method inside the controller.

So in this post, you will learn how to get different route methods differently in the laravel and core PHP.

I have given all possible ways to get the route method Just follow the below given example and learn how to get the route method in different ways.

Core Use

This is a core PHP function using $_SERVER variable you can fetch many details about arrived request into the server.

The key of the $_SERVER variable is REQUEST_METHOD, Using this key inside the server variable you can get your request method.

$_SERVER['REQUEST_METHOD'];

This is a code PHP variable you can use in laravel also. But laravel provides another method for that.

Using isMethod()

If you want to know the request method in laravel and want action according to the request then the below code will help you. You can see below the example code with the various methods.

\Request::isMethod('get');

\Request::isMethod('post');

\Request::isMethod('patch');

\Request::isMethod('delete');

isMethod() will return a boolean if the given arguments existed with the coming request method.

Using method()

The method() method will return the existing request method in string. like "GET", "POST" etc.

$method = \Request::method();

dd($method);

There is no need to pass any arguments inside the method() method.

Request Class

If you are working inside the request validation class then it is quite different from the controller.

using $this notetion you need to get that method name. You can see in below code demo example

$this->method();

This is an example of how we can use it in request class.

public function rules()
{   
    if($this->method() == "PATCH"){
        return [
            'message' => 'nullable',
        ];
    }else{
        return [
            'message' => 'required',
        ];
    }
}

The not only method, You call form data Using $this notation in request class like $this->name.

Different Route Methods

These all different route methods available in laravel.

Laravel Routing Multiple Method Use In Single Route

Route::get('your-url','YourController@yourControllerMethod');
Route::post('your-url','YourController@yourControllerMethod');
Route::patch('your-url','YourController@yourControllerMethod');
Route::put('your-url','YourController@yourControllerMethod');
Route::delete('your-url','YourController@yourControllerMethod');
Route::match(['get','post'],'your-url','YourController@yourControllerMethod');
Route::any('your-url','YourController@yourControllerMethod');
Route::options('your-url','YourController@yourControllerMethod');

Route Resource Controller Laravel


We always thanks to you for reading our blogs.


dharmesh-image

Dharmesh Chauhan

(Swapinfoway Founder)

Hello Sir, We are brothers origin from Gujarat India, Fullstack developers working together since 2016. We have lots of skills in web development in different technologies here I mention PHP, Laravel, Javascript, Vuejs, Ajax, API, Payment Gateway Integration, Database, HTML5, CSS3, and Server Administration. So you need our service Please Contact Us

haresh-image

Haresh Chauhan

(Co-Founder)


We Are Also Recommending You :