【问题标题】:Generating absolute URLs in symfony tasks在 symfony 任务中生成绝对 URL
【发布时间】:2010-07-26 13:25:11
【问题描述】:

我有一个关于在 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;
  }

这允许我通过切换上下文来为任何应用程序创建一个 URL。我遇到的最大问题是在 symfony 任务中创建绝对 URL 时。 在任务中创建路线时,我得到以下信息:

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

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

网址应该是这样的:

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

有没有人能够创建一个代表来自 symfony 任务的绝对 URL 的路由?如果有,您是否也遇到过这个问题,您是如何克服的?

【问题讨论】:

    标签: symfony1


    【解决方案1】:

    documentation 回答了这个问题。编辑 factory.yml 配置文件即可:

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

    【讨论】:

    • 这是完美的答案!现在我什至可以在任务中使用url_for
    • 我喜欢这个答案,因为它很简单。请记住,devprod 环境中的主机可能不同。
    • 同意@Tapper,这应该只被视为一种黑客行为。
    【解决方案2】:

    请参阅this similar question 我在其中发布了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;
     }
    

    这个方法被添加到我们的基础任务类中,我们在其中添加了其他常见的项目特定任务方法。

    【讨论】:

      【解决方案3】:

      看起来你也可以只在你的任务中欺骗路由:

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

      这样您就不必更改主配置。

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 2017-12-10
        • 2011-05-22
        • 1970-01-01
        • 2011-07-29
        • 2019-02-11
        • 1970-01-01
        • 2023-03-28
        • 1970-01-01
        相关资源
        最近更新 更多