我在跑步SymfonyUbuntu 10.0.4 LTS 上的 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);
}
路由名称定义如下:
foobar:
url: /some/fancy/path/:cow/:id/hello.html
param: { module: mymodule, action: myaction }
运行时,生成的链接为:
Line 1产生这个输出:
./symfony/symfony/some/fancy/path/marymoo/42/hello.html
而不是预期的:
/some/fancy/path/marymoo/42/hello.html
Line 2产生错误:
无法找到匹配的路由
为参数“数组(
'动作' => 'symfony', '模块' =>
'.',)"。
同样,预期的 URL 是:
/some/fancy/path/marymoo/42/hello.html
我该如何解决这个问题?
要在任务中生成 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;
}
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)