tags:

views:

172

answers:

1

Hi, I have a quick question regarding the creation of absolute URLs within a symfony task. Basically I have the following:

  /**
   * This function returns the link to a route
   *
   * @param $context context from which to create the config from
   * @param $routingText this contains the text to be routed
   * @param $object if we are generating an object route we need to pass the object
   * @param boolean $absolute - whether to generate an absolute path
   * @return string
   */
  public static function getUrlFromContext($routingText, $object = null, $application = null, $debug = false,
                                           $absolute = false, $htmlSuffix=true)
  {
    $currentApplication = sfConfig::get('sf_app');
    $currentEnvironment = sfConfig::get('sf_environment');
    $context = sfContext::getInstance();
    $switchedContext = false;
    if (!is_null($application) && $context->getConfiguration()->getApplication() != $application)
    {
      $configuration = ProjectConfiguration::getApplicationConfiguration($application, $currentEnvironment,
                      $debug);
      $routing = sfContext::createInstance($configuration)->getRouting();
      $switchedContext = true;
    }
    else
    {
      $routing = $context->getRouting();
    }
    if (is_object($object))
    {
      $route = $routing->generate($routingText, $object, $absolute);
    }
    else
    {
      $route = $routing->generate($routingText, null, $absolute);
    }

    if ($switchedContext)
    {
      sfContext::switchTo($currentApplication);
    }

    if (strcasecmp($application, 'frontend') == 0 && strcasecmp($currentEnvironment, 'prod') == 0)
    {
      $route = preg_replace("!/{$currentApplication}(_{$currentEnvironment})?\.php!", $application, $route);
    }
    else
    {
      $route = preg_replace("/$currentApplication/", $application, $route);
    }

    return $route;
  }

This allows me to create a URL for any application simply by toggling the context. The big problem I'm having is when creating absolute URLs in a symfony task. When creating a route in a task I am getting the following:

http://./symfony/symfony/omg-news/omg-news-channel/test002.html

My assumption is that symfony is trying to guess the domain name from the referrer, which when using symfony tasks is non-existent.

The URL is supposed to look like this:

http://trunk.dev/frontend_dev.php/omg-news/omg-news-channel/test002.html

Has anyone been able to create a route which represents an absolute URL from a symfony task? If so have you also encountered this problem, how did you manage to overcome it?

A: 

See this similar question where I posted the following answer:

 /**
 * Gets routing with the host url set to the url of the production server
 * @return sfPatternRouting
 */
 protected function getProductionRouting()
 {
   $routing = $this->getRouting();
   $routingOptions = $routing->getOptions();
   $routingOptions['context']['host'] = 'www.example.com';
   $routing->initialize($this->dispatcher, $routing->getCache(), $routingOptions);
   return $routing;
 }

This method is added to our base task class where we add other common project specific task methods.

jeremy