2010-07-19 46 views
7

我正在運行Symfony 1.3.6在Ubuntu 10.0.4 LTS上。在Symfony任務中使用路由生成URL

我寫了一個Symfony任務,生成一個包含鏈接(URL)的報告。

這裏是​​方法在我的任務類的代碼段:

protected function execute($arguments = array(), $options = array()) 
    { 
    //create a context 
    sfContext::createInstance($this->configuration); 
    sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url', 'Asset', 'Tag')); 

    ... 
    $url = url_for("@foobar?cow=marymoo&id=42"); 

    // Line 1 
    echo '<a href="'.$url.'">This is a test</a>'; 

    // Line 2 
    echo link_to('This is a test', $url); 
    } 

路線名稱這樣定義:

foobar: 
    url: /some/fancy/path/:cow/:id/hello.html 
    param: { module: mymodule, action: myaction } 

當這個運行時,生成的鏈接是:

1行產生此輸出:

./symfony/symfony/some/fancy/path/marymoo/42/hello.html

,而不是預期:

/some/fancy/path/marymoo/42/hello.html

線2產生一個錯誤:

Unable to find a matching route to generate url for params "array ( 'action' => 'symfony', 'module' => '.',)".

再次,預期網址是:

/some/fancy/path/marymoo/42/hello.html

我怎麼可能會解決這個?

回答

17

爲了生成任務的URL:

protected function execute($arguments = array(), $options = array()) 
{ 
    $routing = $this->getRouting(); 
    $url = $routing->generate('route_name', $parameters); 
} 

我們添加到生成路由的方法,使生產URL總是使用:

/** 
    * 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; 
    } 
+0

+1爲漂亮,簡潔的片段,它告訴我如何解決這個問題。我會稍微修改代碼以適應我的工作,測試它,如果它有效,我會接受這個答案。 – morpheous 2010-07-20 07:03:08

+0

它的工作原理!謝謝你,謝謝,謝謝! :) – morpheous 2010-07-20 08:36:57

+0

我應該在哪裏放這個片段?我不能在任務中調用$ this-> getRouting():/ – JavierIEH 2012-10-22 14:00:33

3

我你想使用標準的幫手(如url_for)生成URL,也許這代碼可以幫助你:

protected function execute($arguments = array(), $options = array()) 
    { 
    // initialize the database connection 
... 
$context = sfContext::createInstance($this->configuration); 

$routing = $context->getRouting(); 
$_options = $routing->getOptions(); 
$_options['context']['prefix'] = "";// "/frontend_dev.php" for dev; or "" for prod 
$_options['context']['host'] = sfConfig::get('app_url_base'); 
$routing->initialize($this->dispatcher, $routing->getCache(),$_options); 
$context->getConfiguration()->loadHelpers('Partial'); 
$context->set('routing',$routing);  
//$_SERVER['HTTP_HOST'] = sfConfig::get('app_url_base'); // ---> I don't remember why I have this on my code, shouldn't be necessary 
... 

然後,您可以使用帶有絕對= true參數url_for功能到處奇蹟般地工作。

當然,你需要在你的app.yml中添加* url_base *定義(或者你可以把它harcoded)

+2

thansk很多,這爲我工作。小評論:'$ options'用於任務命令行選項,所以變量的名稱不同可以避免問題.. – Tapper 2012-10-30 16:34:18

+0

已更新。謝謝。 – glerendegui 2012-11-01 15:48:22

0

你可以調整默認請求選項爲項目配置腳本命令(sfTask) config/ProjectConfiguration.class.php

class ProjectConfiguration extends sfProjectConfiguration { 

    public function setup() { 
     ... 
     $this->dispatcher->connect('command.pre_command', array('TaskWebRequest', 'patchConfig')); 
    } 

} 

class TaskWebRequest extends sfWebRequest { 

    public function __construct(sfEventDispatcher $dispatcher, $parameters = array(), $attributes = array(), $options = array()) 
    { 
     $options['no_script_name'] = true; 
     $options['relative_url_root'] = ''; 
     parent::__construct($dispatcher, $parameters, $attributes, $options); 
    } 

    public static function patchConfig(sfEvent $event) { 
     sfConfig::set('sf_factory_request', 'TaskWebRequest'); 
    } 

}