0
// route
TestTestAppBundle_foobar:
    pattern:  /foobar
    defaults: { _controller: TestTestAppBundle:Default:fooar }


//controller

public function foobarAction(Request $request)
    {
        $request = $this->getRequest();
        $method = $request->getMethod();
        $var =  $request->query->keys();
        $response = new Response('Content', 200, array('content-type' => 'text/html'));
        $response->setContent($var);
        return $response;
    }

When I am making a call to URL

/foobar?foo=bar

then it returns empty array. Instead it should return the GET parameters.

How to handle GET request in symfony?

1
  • I found the solution way back, It was nginx and php-fpm issue, nginx was removing all the query parameter. Commented Jul 13, 2012 at 7:15

2 Answers 2

2

Try adding a slash in pattern e.g

pattern:  /foobar/

It will also ensure that route will match both /foobar and /foobar/ pattern.

Sign up to request clarification or add additional context in comments.

Comments

2

It won't ever work like this for 2 reasons:

  • As far as I can see, it's getting query string as it should, but setContent is bit messed up. First argument shouldn't be array, but string. So $var[0] will work and set content to foo.

  • Also, your routing is bit wrong. Your pattern is /foobar, but you are trying to open /foobar/ which isn't the same. If you set pattern: /foobar/, you could use both /foobar/ and /foobar (which in fact redirects to /foobar/).

However, query->keys() this will return just keys in this case. If you want to get both keys and values, you should use query->all() or query->get('foo') for value of foo only.

Also, you don't need to use $request = $this->getRequest(); as request is already first argument in your action. You can just use it straight away.

setContent is also bit useless in this case as first argument of Response already calls it, so you can set it there without using extra function call.

7 Comments

In poster's defence, I think he is experimenting with the framework. Also he wanted to print the GET parameters as output. I think there isn't any problem there. As $var is null, He did not get the exception on setting Response.
@m2mdas Yes . I am experimenting with the framework. And checking every thing by printing
@Tauquir Where exactly is that NULL printed? I don't see any var_dump anywhere. Could you update the question with exact steps you took? It wouldn't print "NULL" if $var was NULL. @m2mdas I'm fully aware of that. I just took the chance and gave some tips to him :)
I mean null is nothing. Yes It is not printing any thing.
Are you trying on dev or prod environment? If on prod, try to clear cache if you haven't done so, it could help. I noticed also typo in TestTestAppBundle:Default:*fooar*, that could cause also blank screen in prod environment.
|

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.