开发者

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
开发者_如何学C

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".


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();


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

routing:
  class: sfPatternRouting
  param:
    extra_parameters_as_query_string: true


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
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜