开发者

Generating absolute URLs in symfony tasks

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,
                                    开发者_如何学C       $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?


The documentation answers this question. It's enough to edit the factories.yml configuration file:

all:
  routing:
    class: sfPatternRouting
    param:
      generate_shortest_url:            true
      extra_parameters_as_query_string: true
      context:
        host: example.org


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.


Look like you can also trick the Routing only in your task:

sfConfig::set('sf_factory_request_parameters', array('relative_url_root' => "", 'no_script_name' => true));
sfContext::createInstance($this->configuration);

This way you do not have to alter your main config.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜