Cross apps url helper for symfony 1.0

Here's a helper that allow to generate cross apps urls in symfony 1.0

/**
 * Generates cross-apps urls in symfony 1.0
 *
 * @param  string  the app we want to go to
 * @param  string  the route in the app. Must be valid
 * @param  array   the arguments required by the route. Optional
 * @return string
 */
function cross_app_url($app, $route, $args = null)
{
  $host = sfContext::getInstance()->getRequest()->getHost() ;
  $env = sfConfig::get('sf_environment');
  $appRoutingFile = SF_ROOT_DIR.DIRECTORY_SEPARATOR.'apps'.DIRECTORY_SEPARATOR.$app.DIRECTORY_SEPARATOR.'config'.DIRECTORY_SEPARATOR.'routing.yml' ;
  $route = substr($route, 1, strlen($route)) ;
  if (file_exists($appRoutingFile))
  {
    $yml = sfYaml::load($appRoutingFile) ;
    $routeUrl = $yml[$route]['url'] ;
    if ($args)
    {
      foreach ($args as $k => $v)
      {
        $routeUrl = str_replace(':' . $k, $v, $routeUrl) ;
      }
    }
    if (strrpos($routeUrl, '*') == strlen($routeUrl) - 1)
    {
      $routeUrl = substr($routeUrl, 0, strlen($routeUrl) - 2) ;
    }
  }
  return sprintf('http://%s/%s/%s',
                 $host,
                 ($env == 'dev' ? $app . '_dev.php' : ($app != $main_app) ? $app : ''),
                 $routeUrl);
}

Adapted from here

by Nicolas Perriault on 2008-05-15, tagged link  routing  symfony  url 

Comments on this snippet

gravatar icon
#1 Hugues L on 2008-06-03 at 11:48

Bonjour,

je vois pas le role de

<code> $route = substr($route, 1, strlen($route)) ; </code>

As tu des exemples d'appel de ce helper ?

gravatar icon
#2 Nicolas Perriault on 2008-06-03 at 03:10

C'est pour virer le @ du nom de la route

You need to create an account or log in to post a comment or rate this snippet.