简体   繁体   English

在symfony 1.4任务中生成URL

[英]Generate URL in a symfony 1.4 task

I have to generate an URL in a task but I get an incorrect url of type: 我必须在任务中生成一个URL,但我得到一个不正确的类型的URL:

./symfony/user/edit/id

when I need 当我需要的时候

/user/edit/id

My first try was: 我的第一次尝试是:

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));
    // ...
}

My second try, following this answer gives the same incorrect url: 我的第二次尝试,按照这个答案给出了相同的错误网址:

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));
    // ...
}

How can I get the correct URL ? 我怎样才能获得正确的URL?

Have you tried the solution from the snippet ? 您是否尝试过该片段中的解决方案? I use this one in many projects. 我在很多项目中都使用过这个。

I was also using almost the same second solution you describe but a bit different: 我也使用了你描述的几乎相同的第二种解决方案,但有点不同:

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

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

Check parameters for genUrl , you can give true/false as second argument for absolute url. 检查genUrl参数,您可以将true / false作为绝对URL的第二个参数。

I solved adding the --application option! 我解决了添加--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'),
    ));

    //...
}

I didn't know that it was required even if I write directly its name in the getApplicationConfiguration params. 即使我在getApplicationConfiguration参数中直接写了它的名字,我也不知道它是必需的。

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM