1

I have to generate an URL in a task but I get an incorrect url of type:

./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 ?

Community
  • 1
  • 1
Visavì
  • 2,333
  • 1
  • 21
  • 29

2 Answers2

2

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.

j0k
  • 22,600
  • 28
  • 79
  • 90
  • thanks, your solution don't work for me (same wrong url, also with absolute url)... will try the snippet – Visavì Nov 23 '12 at 11:12
2

I solved adding the --application option!

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.

$configuration = ProjectConfiguration::getApplicationConfiguration('frontend', $options['env'], true);
Visavì
  • 2,333
  • 1
  • 21
  • 29