19

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)

NeuronQ
  • 7,527
  • 9
  • 42
  • 60

2 Answers2

20

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');
lukasgeiter
  • 147,337
  • 26
  • 332
  • 270
-4

Why trying to complicate things when you can do easily what you need :

$posted = $_POST;
  • 3
    1. Not good for unit/acceptance tests. 2. Not good for components that depends from Illuminate\Http\Request (i.e. if you will use ReactPHP adapter for Laravel/Lumen). – barbushin Oct 21 '17 at 09:52
  • 4
    Whenever possible, you shouldn't directly access $_GET or $_POST fields, as they might contain unescaped data. – Dragas Oct 25 '17 at 12:14