framework icon indicating copy to clipboard operation
framework copied to clipboard

[Proposal] Add isPost, isGet etc methods to Illuminate\Http\Request

Open alexdover opened this issue 12 years ago • 7 comments

I found myself using the following code a few times in an application when I wanted to use the same controller method for both get and post requests:

if( Request::getMethod() == 'POST')

I thought it might be more elegant to be able to write:

if( Request::isPost())

Could also then add isGet, isPut etc..

alexdover avatar Jan 08 '14 14:01 alexdover

Maybe a Request::is('GET') ?

usirin avatar Jan 08 '14 15:01 usirin

Request::is('admin/*') is already there.

crynobone avatar Jan 08 '14 15:01 crynobone

Sorry about my suggestion based on lack of my knowledge then. Request::isPost() or Request::isGet are better solutions, and even though it can be achieved by the getMethod, i believe those would be more elegant solutions.

usirin avatar Jan 08 '14 15:01 usirin

The is method already exists for checking the requested uri against a pattern.

alexdover avatar Jan 08 '14 15:01 alexdover

Symfony Request is already have a method named isMethod. You can see it here:

https://github.com/symfony/HttpFoundation/blob/master/Request.php#L1404

I made a test in Laravel if it is working already and it does.

So you can make:

if( Request::isMethod('POST') ) instead of if( Request::getMethod() == 'POST')

usirin avatar Jan 08 '14 16:01 usirin

So there is no need for this proposal, perhaps consider making a pull request to docs if you want to add it.

riotCode avatar Jan 08 '14 17:01 riotCode

Yes, you are right there is no need for this proposal if the underlying Symfony class has a suitable method. I will take a look at the docs and see if I can see a natural place for this detail to potentially be added.

Maybe there is a wider question about whether the Laravel docs could do more to point people to additional methods which are available from the Symfony classes.

alexdover avatar Jan 08 '14 17:01 alexdover