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:

<code>
my_rule:
  url:   /call_my_rule
  param: { module: mymodule, action: myaction }
</code>

Use the following calls in the action:

<code>
// 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&param2=yyy'
 
$uri = sfRouting::getInstance()->getCurrentInternalUri(true);
// will return '@myrule?param1=xxx&param2=yyy'
 
$route = sfRouting::getInstance()->getCurrentRouteName();
// will return 'myrule'
</code>

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

コメントを残す

メールアドレスが公開されることはありません。 が付いている欄は必須項目です

このサイトはスパムを低減するために Akismet を使っています。コメントデータの処理方法の詳細はこちらをご覧ください