Matt Huggins
Matt Huggins

Reputation: 83279

How to pass normal parameters in URL in Symfony?

I'm trying to pass parameters via a normal querystring, as such:

/user/login?foo=bar&abc=123

Unfortunately, nowhere does any data in an instance of sfRoute seem to contain data for params foo or abc. How do I fix this?

Edit: Here is the code I'm using as per Tom's request:

/apps/api/config/routing.yml:

login:
  url:   /user/login
  param: { module: user, action: login }

/apps/api/modules/user/actions/actions.class.php:

class userActions extends sfActions {
    public function executeLogin(sfWebRequest $request) {
        echo '<pre>'.print_r($this->getRoute(), true).'</pre>';
    }
}

That's it. The output shows that $this->getRoute() contains no info about foo or abc when I pass them in my query string with the URL "/user/login?foo=bar&abc=123".

Upvotes: 3

Views: 10813

Answers (3)

COil
COil

Reputation: 7596

Note that if you want to use the "extra_parameters_as_query_string: true" parameter you have to delete the final star of your route:

default:
  url:   /:module/:action

Upvotes: 1

prodigitalson
prodigitalson

Reputation: 60413

You need to adjust this in factories.yml i think:

routing:
  class: sfPatternRouting
  param:
    extra_parameters_as_query_string: true

Upvotes: 1

Tom
Tom

Reputation: 30698

When accessing the variables, use the $request object, not sfRoute:

$request->getParameter('foo')

Do make sure the function in the action that's receiving those request params declares it as an incoming variable:

public function executeSomeAction($request)  {  }

If you're looking for the equivalent of $_SERVER['QUERY_STRING'] in Symfony, I haven't found it and would be interested myself.

UPDATE:

I think the method you're using would only print the route. I think what you'll need to do to achieve this is to access them through the $request object, as I mentioned earlier. For example:

$params_typed = $request->getParameterHolder();  // ... and grab them from here

... or use server query string or handle the incoming params individually.

Sorry I can't be of more help.

SECOND UPDATE:

Actually, just tested a little idea:

if you define the params in your routing.yml like this:

login:
  url:   /user/login
  param: { module: user, action: login, foo: something, abc: something }

You can access them via:

$full_path = sfContext::getInstance()->getRouting()->getCurrentInternalUri();

Upvotes: 2

Related Questions