Laravel Routing Multiple Method Use In Single Route

  • 17-06-2022
  • 612
  • Laravel 7
  • Haresh Chauhan

Routing in laravel is very easy and flexible. You can use and define routes in many different ways with some other modifier, each route defined with a different purpose or better situation to specifc cases.

Sometimes you may need to define or be required to register multiple methods in the single route that responds to multiple HTTP requests So you may need to use match in your route.

The Simplest way to accept multiple HTTP request methods using single route is to use the routing match method so Route::match()

So here Below example given it will accept both methods PATCH and POST this both method will accept by given example.

Also you can modify as per your requirement the is no specific route match method fix and also use DELETE and GET method.

Route Example 1

Route::match(['PATCH', 'POST'], 'user/personal-detail/{id?}','UserController@userPersonalDetail');

Route Example 2

Route::match(['GET', 'DELETE'], 'user/{id}','UserController@userGetOrDelete');

Route Example 3

Sometimes possible that there is no specific route method given the HTTP the request comes with any method. it is not a prefix that you want to allow the request.

any() laravel routing method will help you handle and allow you to handle all types of requests from the HTTP.

Route::any('/', function () {
    //
})

When we define multiple routes method that will define the same URL in all the given different route methods like get, post, patch, delete and options method that should define before you call routing method call match, any and then it will redirect ensure route method.

Available Router Methods

These all are routes methods that allow you to register routes that response HTTP verb.

Route::get($uri, $callback);
Route::post($uri, $callback);
Route::put($uri, $callback);
Route::patch($uri, $callback);
Route::delete($uri, $callback);
Route::options($uri, $callback);

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 :