Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

[5.5] Add ->post() method to Request #20238

Merged
merged 2 commits into from
Jul 25, 2017
Merged

[5.5] Add ->post() method to Request #20238

merged 2 commits into from
Jul 25, 2017

Conversation

svenluijten
Copy link
Contributor

The ->post() method will get all data from the request body (POST body), or when given a $key, it'll get that specific value. Using Symfony's public $request property on the Request object: https://github.com/symfony/http-foundation/blob/master/Request.php#L76-L81

@taylorotwell
Copy link
Member

When would you actually use this?

@svenluijten
Copy link
Contributor Author

Given you're on /foo?page=1, with a form that looks like this:

<form action="" method="POST">
    <input name="page" type="text">
    <input type="submit" value="Submit">
</form>

In this case, $request->get('page') would only contain one of the values (either 1 from the query string, or the data you submitted via the form).

With this PR merged, you can safely assume all the data you retrieved with $request->post('page'); is always equal to what was submitted via the form. Maybe post isn't the right word to use here though, since it gets values from the request body (also on PUT, PATCH, etc.). Maybe ->body();?

@dbpolito
Copy link
Contributor

I like this, i already had to workaround with something like this before.

@antonkomarev
Copy link
Contributor

antonkomarev commented Jul 24, 2017

@svenluijten Why this so important for you to get exactly $_POST data? Wouldn't $request->input('page') prefer $_POST over the $_GET params?

@cwhite92
Copy link

cwhite92 commented Jul 24, 2017

I needed this because I wanted to log certain properties of the request, like any query strings that were passed, any POSTed data, the URL etc. But the query strings and POST data needed to be logged separately.

If I used ->input() to get the POST data, it would also include query params. ->all() would also include any query params on the request. There doesn't seem to be a convenient method to retrieve JUST the POSTed data from the request, without merging the query params as well.

I think there's a use-case for this, just as there's a use case for the ->query() method returning just the query params. Right now I've had to resort to doing $request->request->all(), which falls back to Symfony's Request class and returns all POST data without query params.

@antonkomarev
Copy link
Contributor

@cwhite92 that make sense

@tillkruss tillkruss changed the title Add ->post() method to Request [5.5] Add ->post() method to Request Jul 24, 2017
@devcircus
Copy link
Contributor

devcircus commented Jul 24, 2017

$request->request returns $_POST parameters only

@cwhite92
Copy link

cwhite92 commented Jul 24, 2017 via email

@devcircus
Copy link
Contributor

So $request->post() instead of
$request->request

and
$request->post('title') instead of
$request->get('title) ?

@svenluijten
Copy link
Contributor Author

@devcircus No, $request->post(); instead of $request->request->all();, and $request->post('title'); instead of $request->request->get('title');.

$request->get('title'); will include results from the query string, which is not what we want.

@devcircus
Copy link
Contributor

Yeah just seems like the functionality is already there. Lots of PRs lately that simply save a few keystrokes.

Then again, I've never had the same parameters in POST and GET at the same time, so never had the need for separating it like this, maybe it'll come in handy.

@garygreen
Copy link
Contributor

I think this makes a lot of sense. I mean we already have ->query() which get's the query parameters, why not ->post() which get's the post's parameters.

@decadence
Copy link
Contributor

+1 for this. I don't like that we can't take input only from POST.
But this and query don't support dot key.

@taylorotwell taylorotwell merged commit 71f30fe into laravel:master Jul 25, 2017
@svenluijten svenluijten deleted the post-method-request branch July 25, 2017 10:32
@sisve
Copy link
Contributor

sisve commented Jul 26, 2017

What would ->post() return for GET requests? Isn't Request.php#L353 replacing the container you presume to contain post-data with the query string, or perhaps even parsed json data?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

9 participants