Mar 05 2009

Finding the current internal route or URI

Category: RoutingJeff @ 7:49 am
      

I was creating a login form on one of my site pages. The login box appears on all pages of the site until the visitor becomes a user tat is signed in. I wanted to be able to return the user to the exact page they logged in from, so I needed to grab the internal route for any given page, at any time within my layout template file. The method for doing this has changed since 1.0 and now the current method (as of 1.2) goes like this:

sfContext::getInstance()->getRouting()->getCurrentInternalUri();

That gives you the current route. So I included a hidden input in my login form that looked like this:

<input type="hidden" name="route" value="<?php echo sfContext::getInstance()->getRouting()->getCurrentInternalUri(); ?>" />

And now, I can simply redirect the user back to the page they logged in from by grabbing that route variable.

This is a bit more you can do with this to take it a step further for your needs. I’m simply copying from the Symfony book site:

// 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');

4 Responses to “Finding the current internal route or URI”

  1. Vit228 says:

    Is there any way to get ModelName for current module/action from backend/layout.php?

  2. Jeff says:

    I’m not sure I follow what you’re asking. When you say ModelName, what are you referring to… and what exactly are you trying to do? Happy to help if I can :)

  3. Vit228 says:

    You see, most modules in backend are based on exact Model, and it is described in routing.yml

    article:
    class: sfPropelRouteCollection
    options:
    model: Article
    module: article
    prefix_path: article
    column: id
    with_wildcard_routes: true

    I need to retrieve this option:
    model: Article

    I saw saw model-name as option in:
    class sfPropelRoute extends sfObjectRoute
    public function __construct($pattern, array $defaults = array(), array $requirements = array(), array $options = array())
    {
    parent::__construct($pattern, $defaults, $requirements, $options);
    $this->options['object_model'] = $this->options['model'];
    $this->options['model'] = constant($this->options['model'].’::PEER’);

    but I didn findout how to retieve this option :’(

  4. Jeff says:

    I see what you mean. Sorry to say though that I haven’t personally played much with that area of the framework’s capabilities so I don’t have an answer for you off the top of my head. If you do find your solution, it would be great if you could post it back as a comment here. Best of luck!

Leave a Reply