The symfony Cookbook

How to create Links between Applications

You are currently browsing
the website for symfony 1

Visit the Symfony2 website


About

You are currently reading "The symfony Cookbook" which is licensed under the Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License license.

Master symfony

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Books on symfony

Learn more about symfony with the official guides.
books.sensiolabs.com

L'audit Qualité par SensioLabs

200 points de contrôle de votre applicatif web.
audit.sensiolabs.com
symfony training
Be trained by symfony experts
Feb 21: Köln (Getting Started with Symfony2 - English)
Feb 27: Köln (Mastering Symfony2 - English)
Mar 05: Köln (Web Development with Symfony2 - Deutsch)
Mar 05: Montreal (Web Development with Symfony2 - English)
Mar 05: Montreal (Getting Started with Symfony2 - English)
and more...

Search


powered by google
You are currently browsing "The symfony Cookbook" in English for the 1.2 version - Switch to version: - Switch to language:
Creative Commons License This work is licensed under a Creative Commons Attribution-Noncommercial-No Derivative Works 3.0 Unported License.
Translation of this work into another language is explicitly allowed.
This version of symfony is not maintained anymore.
If some of your projects still use this version, consider upgrading as soon as possible.

A symfony project is made of one or more applications. Applications share nothing, but the model classes. But sometimes, you need the ability to create links to a frontend application from a backend one. Think about a CMS backend where you want the user to edit an article and then link to the corresponding article in the frontend.

In this tutorial, I will show you a simple solution to this problem.

Beside parsing the configuration files and converting the resulting array to a PHP cache, some configuration handler classes can also "evaluate" the configuration for direct consumption (the autoload, database, and routing configuration handlers sports this new behavior).

For example, if you want to convert a file containing route definitions in the YAML format to their corresponding routes objects, you can do something like the following:

$config = new sfRoutingConfigHandler();
$routes = $config->evaluate(array('/path/to/routing.yml'));

If you execute the above code snippet, the $routes array will be populated with an array of route objects, equivalent to the route definitions in the YAML file. The generate() method takes an array of YAML filenames as its first argument and merge the content of the files.

The evaluate() method takes an array of configuration files. If you have some plugins that define routes in a routing.yml file, add its file path in the array and the configuration will be merged with the main configuration.

Thanks to the framework decoupling and the sfPatternRouting::setRoutes() method, you can easily create a frontend routing object from any PHP script:

$routing = new sfPatternRouting(new sfEventDispatcher());
$routing->setRoutes($routes);

Generating URLs is now as simple as calling the routing object generate() method:

$routing->generate('homepage');
$routing->generate('article', array('id' => $id));

The generate() method takes a route name as its first argument, and an array of parameters as its second one.

Let's use this knowledge to simplify the creation of frontend URLs from a backend application.

In the backendConfiguration class, add the following code:

// apps/backend/config/backendConfiguration.class.php
class backendConfiguration extends sfApplicationConfiguration
{
  protected $frontendRouting = null;
 
  public function generateFrontendUrl($name, $parameters = array())
  {
    return 'http://frontend.example.com'.$this->getFrontendRouting()->generate($name, $parameters);
  }
 
  public function getFrontendRouting()
  {
    if (!$this->frontendRouting)
    {
      $this->frontendRouting = new sfPatternRouting(new sfEventDispatcher());
 
      $config = new sfRoutingConfigHandler();
      $routes = $config->evaluate(array(sfConfig::get('sf_apps_dir').'/frontend/config/routing.yml'));
 
      $this->frontendRouting->setRoutes($routes);
    }
 
    return $this->frontendRouting;
  }
 
  // ...
}

Notice that the generateFrontendUrl() method always generates absolute URLs for obvious reasons. This is the only information that still need to be hardcoded, as symfony does not have this knowledge and cannot guess it.

With this code in place, you can now generate a frontend URL from anywhere in the backend. Here is for instance how to redirect the user to the frontend from a backend action:

$this->redirect($this->getContext()->getConfiguration()->generateFrontendUrl('hello', array('name' => 'Bar')));

You can also create a small helper for templates:

function link_to_frontend($name, $parameters)
{
  return sfProjectConfiguration::getActive()->generateFrontendUrl($name, $parameters);
}

That's all there is to it!

If you have many applications, it is pretty easy to refactor the above code to generate URL for and from any other application.

The technique described in this post is used internally by symfony for the app:routes tasks.

Questions & Feedback

If you find a typo or an error, please register and open a ticket.

If you need support or have a technical question, please post to the official user mailing-list.