7

I have a Symfony\Component\HttpFoundation\Request object and from it I want to fetch all the url paramethers that provided. In other words when the user visits the http://example.org/soempage?param1=value1&param2=value2&param3=value3

I want to generate an array that has these values ['param1','param2','param3'] .

Also I have seen this one: How to get all post parameters in Symfony2?

And I tried the following based on above:

    /**
     * @var request Symfony\Component\HttpFoundation\Request
     */
    $parametersToValidate=$request->request->all();
    $parametersToValidate=array_keys($parametersToValidate);

And

    /**
     * @var request Symfony\Component\HttpFoundation\Request
     */
    $parametersToValidate=$request->all();
    $parametersToValidate=array_keys($parametersToValidate);

Without the desired result but instead I get this error message:

Attempted to call an undefined method named "all" of class "Symfony\Component\HttpFoundation\Request\

Edit 1

The request I use int into a static method that validates my input. The method is called via the controller and is implemented like that for reusability purposes.

public static function httpRequestShouldHaveSpecificParametersWhenGiven(Request $request,array $parametersThatHttpRequestShouldHave)
    {
        $parametersToValidate=$request->parameters->all();

        if(empty($parametersToValidate)){
            return;
        }
        
        $parameters=array_keys($parametersToValidate);
        
        foreach($parameters as $param){
            if(!in_array($parameters,$parametersThatHttpRequestShouldHave)){            
                throw new InvalidNumberOfParametersException(implode(',',$parametersToValidate),implode(',',$parametersThatHttpRequestShouldHave),implode(',',$diff));
            }
        }
    }
2
  • May we see the entire Controller Action? Commented Apr 8, 2017 at 11:03
  • It is not into a contoller but into an anothewr method that I pass it as parameter. Commented Apr 8, 2017 at 11:07

1 Answer 1

22

Did you try:

$parametersToValidate = $request->query->all();
Sign up to request clarification or add additional context in comments.

3 Comments

Yes that helped
Notice that if you want $_POST params, use : $request->request->all();
Strange that Symfony uses the word 'query' to get GET params, and 'request' to get POST params, when PHP uses REQUEST (through global $_REQUEST var) to get both GET and POST params...

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.