60

In my controller/action:

if(!empty($_POST))
{
    if(Auth::attempt(Input::get('data')))
    {
        return Redirect::intended();
    }
    else
    {
        Session::flash('error_message','');
    }
}

Is there a method in Laravel to check if the request is POST or GET?

Cilan
  • 13,101
  • 3
  • 34
  • 51
JoeLoco
  • 2,116
  • 4
  • 31
  • 59

6 Answers6

181

According to Laravels docs, there's a Request method to check it, so you could just do:

$method = Request::method();

or

if (Request::isMethod('post'))
{
// 
}
Tom
  • 3,654
  • 2
  • 16
  • 23
83

The solutions above are outdated.

As per Laravel documentation:

$method = $request->method();

if ($request->isMethod('post')) {
    //
}
hubrik
  • 999
  • 7
  • 6
25

I've solve my problem like below in laravel version: 7+

In routes/web.php:

Route::post('url', YourController@yourMethod);

In app/Http/Controllers:

public function yourMethod(Request $request) {
    switch ($request->method()) {
        case 'POST':
            // do anything in 'post request';
            break;

        case 'GET':
            // do anything in 'get request';
            break;

        default:
            // invalid request
            break;
    }
}
Dave
  • 3,073
  • 7
  • 20
  • 33
7

Of course there is a method to find out the type of the request, But instead you should define a route that handles POST requests, thus you don't need a conditional statement.

routes.php

Route::post('url', YourController@yourPostMethod);

inside you controller/action

if(Auth::attempt(Input::get('data')))
{
   return Redirect::intended();
}
//You don't need else since you return.
Session::flash('error_message','');

The same goes for GET request.

Route::get('url', YourController@yourGetMethod);
giannis christofakis
  • 8,201
  • 4
  • 54
  • 65
  • 27
    Tom's solution below (not picked) is better – Krynble Jun 04 '14 at 03:51
  • @Krynble You downvoted my answer because it is wrong or because it's not the best? – giannis christofakis Jun 04 '14 at 07:03
  • 3
    Just because I believe it didnt answer the question; you have a point in what you say but for very simple tasks (and when adding server side validation) I believe it's better to handle in a single controller method. – Krynble Jun 28 '14 at 21:28
  • 1
    @Krynble How do you reach your single controller method `Route::any()`? – giannis christofakis Jun 29 '14 at 08:24
  • Yes, I use Route::any() and handle everything in the controller method; including validation and displaying typed form information back, when needed. – Krynble Jul 14 '14 at 14:53
  • I agree with @Krynble. This doesn't answer the question. There are circumstances where your POST and GET routes might do very very similar things. This solution encourages unnecessary code-duplication which potentially increases support maintenance in the long run. The number of up-votes on Tom's answer suggests others recognise this too. – cartbeforehorse Sep 13 '17 at 16:58
  • @cartbeforehorse I don't agree with your arguments. In which circumstances POST and GET do similar things ? What about REST, CRUD operations, POST-REDIRECT-GET. Isn't placing all your code in one method with difference logical paths encourage code-duplication ? What accepted as an answer suggests ? – giannis christofakis Sep 20 '17 at 10:01
  • @giannischristofakis You're free not to agree :-) But I'm still holding my position. Why would the Laravel framework allow you to send POST + GET down the same route if there wasn't a practical purpose? See: https://stackoverflow.com/questions/18326030/how-to-route-get-and-post-for-same-pattern-in-laravel#23235478. – cartbeforehorse Sep 20 '17 at 10:48
  • @Krynble do you need validation in your GET request ? And then when you have an error in your submitted form (I presume it's a POST request) , do you redirect it to the same origin route ? – giannis christofakis Sep 20 '17 at 10:48
  • @giannischristofakis Practical purposes include: viewing a record in a database, but if the user arrives at that screen via a form (rather than a link), he should receive a "job-done" message. Otherwise the GET/POST routes do exactly the same thing. It would be code duplication to have to write 2 routes in this circumstance, and "the same screen" might end up doing "different things" as the developer (and future developers) change the code over time. "Accepted answer" means "it was good enough for the asker's needs at the time". It doesn't necessarily mean the actual question was answered ;-) – cartbeforehorse Sep 20 '17 at 10:51
  • @cartbeforehorse You can misuse a framework in all kind of different ways, that's what my answer oppose. I lost you in your explanation. How "the user arrives at that screen via a form" ? You guess, the majority of the voters was looking for Tom's solution, but I guess not the OP. – giannis christofakis Sep 20 '17 at 12:12
  • You have this situation when using pagination. By default the next page will use the same route the view was called from, so reading a database and paginating the results would would send a get to a method that may also receive a post such as in a search request with parameters. To avoid this you would have to redirect the get request to another method which would do the same database query so duplicating code! – Lee Apr 05 '18 at 14:06
  • @Lee I get what you are say, but there is no duplication unless you choose to implement it this way.Your situation is exceptional cause you use a custom query (for your pagination) and a form that posts the query parameters. The redirection as you said is inevitable but you can avoid to do the database query in you post method (which is redundunt and kind of contradictive cause usually POST method stores data and GET fetches). Just redirect and pass the parameters from the POST to your GET method. I don't get why you downvote an answer that doesn't meet your preferences but still isn't wrong! – giannis christofakis Apr 10 '18 at 12:27
6

$_SERVER['REQUEST_METHOD'] is used for that.

It returns one of the following:

  • 'GET'
  • 'HEAD'
  • 'POST'
  • 'PUT'
Kerel
  • 732
  • 6
  • 20
6

Use Request::getMethod() to get method used for current request, but this should be rarely be needed as Laravel would call right method of your controller, depending on request type (i.e. getFoo() for GET and postFoo() for POST).

Marcin Orlowski
  • 72,056
  • 11
  • 123
  • 141
  • 1
    Isn't that Laravel 3 for `is_post` and `is_get` as Laravel 4 uses camelCase? – SamV Jan 11 '14 at 16:34
  • 1
    Using separate methods for GET and POST may not be desirable. For simple CRUD use cases, using the same method for both GET or POST can reduce code duplication and lower the mental load of development. – William Mar 11 '18 at 02:36