Skip to content

Accessing the front-end routing from the back-end application

by Matthias Noback on March 17th, 2011

As a follow-up on my previous post about accessing the front-end sfI18N object from the back-end application, I now provide you with a way to access the front-end routing, from another application, in my example the backend application.

First, add the method getRouting() to your backendConfiguration class:

class backendConfiguration extends sfApplicationConfiguration
{
  /**
   * Get the routing for the given application
   *
   * Optionally set context values, using the $context parameter
   *
   * @see sfWebRequest::getRequestContext()
   *
   * @param string $application
   * @param array $context
   *
   * @return sfRouting
   */
  public function getRouting($application, array $context = array())
  {
    $current_application = sfContext::getInstance()->getConfiguration()->getApplication();

    sfContext::switchTo($application);

    $factories = sfFactoryConfigHandler::getConfiguration(ProjectConfiguration::getActive()->getConfigPaths('config/factories.yml'));

    $class = $factories['routing']['class'];
    $params = $factories['routing']['param'];

    $params['context'] = array_merge(sfContext::getInstance()->getRequest()->getRequestContext(), $context);

    $routing = new $class($this->getEventDispatcher(), null, $params);

    sfContext::switchTo($current_application);

    return $routing;
  }
}

You should add lazy loading to this method, together with some “internal” caching of the generated routing class (by using a property of the backendConfiguration class). See my other post about generating another application’s sfI18n object for an example of this.

Once you have done this, you can access the routing by calling this method from any action, e.g.:

$routing = $this->getContext()->getConfiguration()->getRouting('frontend');
echo $routing->generate('news_detail', array('id' => 3, 'slug' => 'test'));

// this results in, for example: /news/3/test

From a backend application you may encounter some problems, because the generated route is based on the current request, so the script name is included. A problem also arise if you want to generate absolute URLs and you want to link to another site (or your back-end application is hosted on a subdomain), because the routing uses the current host name by default.

Luckely, the given method getRouting() provides a way to override these “contextual” options (see sfWebRequest::getRequestContext()):

$routing = $this->getContext()->getConfiguration()->getRouting('frontend', array(
  'prefix' => '/frontend_dev.php', // or '' in case you want no script name displayed
  'host' => 'www.symfony-project.org',
));

echo $routing->generate('news_detail', array('id' => 3, 'slug' => 'test'));

// this results in, for example: http://www.symfony-project.org/news/3/test
One Comment
  1. That entry really helped me a lot, now generating Links in Mails generated in the frontend linking to backend-stuff is no problem, and linking to frontend-stuff from the backend or even other apps is no problem anymore.

    Thx!

Leave a Reply

Note: XHTML is allowed. Your email address will never be published.

Subscribe to this comment feed via RSS