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

この回答に続く私の2回目の試行は、同じ間違った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));
    // ...
}

正しい URL を取得するにはどうすればよいですか?

4

2 に答える 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。絶対URLの2番目の引数としてtrue/falseを指定できます。

于 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 の params に直接名前を書いても必須だとは知りませんでした。

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