在symfony任务中生成绝对URL

lux*_*ama 4 symfony1

我有一个关于在symfony任务中创建绝对URL的快速问题.基本上我有以下几点:

  /**
   * 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;
  }
Run Code Online (Sandbox Code Playgroud)

这允许我仅通过切换上下文就可以为任何应用程序创建URL.我遇到的一个大问题是在symfony任务中创建绝对URL.在任务中创建路径时,我得到以下内容:

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

我的假设是symfony试图从引用者猜测域名,这在使用symfony任务时是不存在的.

URL应该如下所示:

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

有没有人能够创建一个代表symfony任务的绝对URL的路由?如果是这样,你也遇到了这个问题,你是如何克服它的?

flm*_*flm 5

文件回答了这个问题.编辑factories.yml配置文件就足够了:

all:
  routing:
    class: sfPatternRouting
    param:
      generate_shortest_url:            true
      extra_parameters_as_query_string: true
      context:
        host: example.org
Run Code Online (Sandbox Code Playgroud)