Using routes to generate URLs in a Symfony task

纵饮孤独 提交于 2019-11-30 07:26:16

To generate a URL in a task:

protected function execute($arguments = array(), $options = array())
{
  $routing = $this->getRouting();
  $url = $routing->generate('route_name', $parameters);
}

We add a method to generate routing so that the production URL is always used:

   /**
   * 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;
  }

I you wanna to use standard helpers (like url_for) to generate url, maybe this code could help you:

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

Then, you can use url_for function everywhere with absolute=true parameter magically working.

Of course, you need to add a *url_base* definition in your app.yml (or maybe you can leave it harcoded)

I have had the same problem and found the following Code Snippet: http://snippets.symfony-project.org/snippet/378

The solution is rather similar, however it extends the ProjectConfiguration. The advantage of this approach is, that it works transparently in modules as as well.

You can tweak default request options for commands (sfTask) in the project configuration script 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');
    }

}
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!