2010-07-26 78 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); 

這樣,您就不必改變你的主要配置。