2010-07-26 81 views
4

关于在symfony任务中创建绝对URL的问题,我有一个疑问。 基本上我有以下几点:在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的任务时,该域名是不可-existent。

的URL应该是这样的:

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

有没有人能够创造出代表从symfony的任务绝对URL路径?如果是的话,你也遇到了这个问题,你是如何克服它的?

回答

5

documentation回答了这个问题。这足以编辑factories.yml配置文件:

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

这是最完美的答案!现在我甚至可以在任务中使用'url_for'! – flocki 2012-07-17 12:46:30

+1

我喜欢这个答案,因为它很简单。请记住主机在'dev'和'prod'环境中可能会有所不同。 – Tapper 2012-10-30 14:25:50

+0

同意@Tapper,这应该只被视为黑客。 – 2015-01-15 01:07:32

1

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; 
} 

此方法添加到我们这里,我们添加其他普通项目的具体任务的方法基本任务类。

1

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

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

这样,您就不必改变你的主要配置。