Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

In Laravel, how can I get *only* POST parameters?

I know that one can use $request->get('my_param') or Input::get('my_param') to get a POST or GET request parameter in Laravel (I'm toying with v5/dev version now, but it's the same for 4.2).

But how can I make sure that my my_param came via a POST parameter and was not just from a ?my_param=42 appended to the URL? (besides reverting to the ol' $_POST and $_GET superglobals and throwing testability out the window)

(Note: I also know that the Request::get method will give me the POST param for a POST request, if both a POST an URL/GET param with the same name exist, but... but if the param land in via the url query string instead, I want a Laravel-idiomatic way to know this)

like image 773
NeuronQ Avatar asked Dec 08 '14 20:12

NeuronQ


People also ask

How do I get params in laravel?

Method 1: $request->route('parameter_name') We can access route parameters in two ways. One way is by using $request->route('parameter_name') ., where parameter_name refers to what we called the parameter in the route.

How can you retrieve the full URL for the incoming request laravel?

The “path” method is used to retrieve the requested URI. The is method is used to retrieve the requested URI which matches the particular pattern specified in the argument of the method. To get the full URL, we can use the url method.

What is get and post method in laravel?

Simply said, GET is usually used for presenting/viewing something, while POST is used to change something. For example, when you fetching data for some user you use GET method and it'll look something like this: Route::get('users/{id}', function($id) { $user = \App\User::find($id); echo "Name: " .


1 Answers

In the class Illuminate\Http\Request (or actually the Symphony class it extends from Symfony\Component\HttpFoundation\Request) there are two class variables that store request parameters.

public $query - for GET parameters

public $request - for POST parameters

Both are an instance of Symfony\Component\HttpFoundation\ParameterBag which implements a get method.

Here's what you can do (although it's not very pretty)

$request = Request::instance();
$request->request->get('my_param');
like image 178
lukasgeiter Avatar answered Sep 30 '22 13:09

lukasgeiter