1

我必须在任务中生成一个 URL,但我得到的 URL 类型不正确:

./symfony/user/edit/id

当我需要的时候

/user/edit/id

我的第一次尝试是:

protected function execute($arguments = array(), $options = array())
{
    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true);
    $this->context = sfContext::createInstance($configuration);
    $baseurl = $this->context->getController()->genUrl(array('module' => 'user','action' => 'edit', 'id' => $id));
    // ...
}

我的第二次尝试,按照这个答案给出了相同的错误网址:

protected function execute($arguments = array(), $options = array())
{

    $configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true);
    $this->context = sfContext::createInstance($configuration);
    $routing = $this->context->getRouting();
    $baseurl = $routing->generate('user_edit', array('id' => $id));
    // ...
}

我怎样才能得到正确的网址?

4

2 回答 2

2

您是否尝试过片段中的解决方案?我在许多项目中使用这个。

我也使用了您描述的几乎相同的第二种解决方案,但有点不同:

// you get the context the same way
$context = sfContext::createInstance($configuration);
$this->controller = $context->getController();

$baseurl = $this->controller->genUrl('user_edit?id='.$id);

检查参数genUrl,您可以将 true/false 作为绝对 url 的第二个参数。

于 2012-11-23T10:54:34.213 回答
2

我解决了添加 --application 选项!

protected function configure()
{
    $this->addOptions(array(
        new sfCommandOption('env', null, sfCommandOption::PARAMETER_REQUIRED, 'The environment', 'dev'),
        new sfCommandOption('application', null, sfCommandOption::PARAMETER_REQUIRED, 'The application name', 'frontend'),
    ));

    //...
}

即使我直接在 getApplicationConfiguration 参数中写入它的名称,我也不知道它是必需的。

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true);
于 2012-11-23T11:49:13.160 回答