使用路由在Symfony任务中生成URL

mor*_*ous 7 symfony1 symfony-1.4

我在Ubuntu 10.0.4 LTS上运行Symfony 1.3.6.

我编写了一个Symfony任务,生成一个包含链接(URL)的报告.

这是execute()我的任务类中方法的片段:

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

路由名称定义如下:

foobar:
  url: /some/fancy/path/:cow/:id/hello.html
  param: {  module: mymodule, action: myaction }
Run Code Online (Sandbox Code Playgroud)

运行此命令时,生成的链接为:

第1行产生此输出:

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

而不是预期的:

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

第2行生成错误:

无法找到匹配的路由来为params生成url"array('action'=>'symfony','module'=>'.',)".

同样,预期的URL是:

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

我怎么解决这个问题?

Jer*_*man 17

要在任务中生成URL:

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

我们添加了一种生成路由的方法,以便始终使用生产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;
  }
Run Code Online (Sandbox Code Playgroud)