[symfony] Retrieve information about the current route

symfony PHP5 framework

Retrieve information about the current route

If you need to retrieve information about the current route, for instance to prepare a future 'back to page xxx' link, you should use the methods of the sfRouting object. For instance, if your routing.yml defines:

my_rule:
  url:   /call_my_rule
  param: { module: mymodule, action: myaction }
Use the following calls in the action:

// if you require an URL like
http://myapp.example.com/call_my_rule/param1/xxx/param2/yyy
 
$uri = sfRouting::getInstance()->getCurrentInternalUri();
// will return 'mymodule/myaction?param1=xxx¶m2=yyy'
 
$uri = sfRouting::getInstance()->getCurrentInternalUri(true);
// will return '@myrule?param1=xxx¶m2=yyy'
 
$route = sfRouting::getInstance()->getCurrentRouteName();
// will return 'myrule'

The URIs returned by the ->getCurrentInternalUri() method can be used in a call to a link_to() helper.

In addition, you might want to get the first or the last action called in a template. The following variables are automatically updated at each request and are available to templates:

$sf_first_action
$sf_first_module
$sf_last_action
$sf_last_module

トラックバック(0)

このブログ記事を参照しているブログ一覧: Retrieve information about the current route

このブログ記事に対するトラックバックURL: http://www.pistolfly.jp/mt/mt-tb-alt.cgi/104

コメントする