7

我在 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); 
  }

路由名称定义如下:

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 行生成错误:

找不到匹配的路由来为参数“array ('action' => 'symfony', 'module' => '.',)”生成 url。

同样,预期的 URL 是:

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

我该如何解决这个问题?

4

4 回答 4

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;
  }
于 2010-07-20T02:49:54.917 回答
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
...

然后,您可以在任何地方使用url_for函数,absolute=true 参数神奇地起作用。

当然,您需要在 app.yml 中添加一个 *url_base* 定义(或者您可以将其保留为硬编码)

于 2012-09-16T18:41:44.763 回答
1

我遇到了同样的问题,发现了以下代码片段:http ://snippets.symfony-project.org/snippet/378

该解决方案非常相似,但是它扩展了 ProjectConfiguration。这种方法的优点是,它也可以在模块中透明地工作。

于 2011-10-05T12:20:42.127 回答
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');
    }

}
于 2015-07-03T04:50:51.143 回答