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 sfPatternRouting
object. The URIs returned by the getCurrentInternalUri()
method can be used in a call to a link_to()
helper, as shown in Listing 9-25.
Listing 9-25 - Using sfRouting
to Get Information About the Current Route
// If you require a URL like
http://myapp.example.com/article/21
$routing = sfContext::getInstance()->getRouting();
// Use the following in article/read action
$uri = $routing->getCurrentInternalUri();
=> article/read?id=21
$uri = $routing->getCurrentInternalUri(true);
=> @article_by_id?id=21
$rule = $routing->getCurrentRouteName();
=> article_by_id
// If you just need the current module/action names,
// remember that they are actual request parameters
$module = $request->getParameter('module');
$action = $request->getParameter('action');
If you need to transform an internal URI into an external URL in an action — just as url_for()
does in a template — use the genUrl()
method of the sfController object, as shown in Listing 9-26.
Listing 9-26 - Using sfController
to Transform an Internal URI
$uri = 'article/read?id=21';
$url = $this->getController()->genUrl($uri);
=> /article/21
$url = $this->getController()->genUrl($uri, true);
=> http://myapp.example.com/article/21