Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using routes to generate URLs in a Symfony task

I am running Symfony 1.3.6 on Ubuntu 10.0.4 LTS.

I have written a Symfony task that generates a report which contains links (URLs).

Here is a snippet of the execute() method in my task class:

  protected function execute($arguments = array(), $options = array())
  {
    //create a context
    sfContext::createInstance($this->configuration);
    sfContext::getInstance()->getConfiguration()->loadHelpers(array('Url', 'Asset', 'Tag'));

    ...
    $url = url_for("@foobar?cow=marymoo&id=42");

    // Line 1
    echo '<a href="'.$url.'">This is a test</a>';

    // Line 2
    echo link_to('This is a test', $url); 
  }

The route name is defined like this:

foobar:
  url: /some/fancy/path/:cow/:id/hello.html
  param: {  module: mymodule, action: myaction }

When this is run, the generated link is:

Line 1 produces this output:

./symfony/symfony/some/fancy/path/marymoo/42/hello.html

instead of the expected:

/some/fancy/path/marymoo/42/hello.html

Line 2 generates an error:

Unable to find a matching route to generate url for params "array ( 'action' => 'symfony', 'module' => '.',)".

Again, the expected URL is:

/some/fancy/path/marymoo/42/hello.html

How may I resolve this?

like image 460
morpheous Avatar asked Jul 19 '10 23:07

morpheous


3 Answers

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;
  }
like image 98
Jeremy Kauffman Avatar answered Nov 20 '22 00:11

Jeremy Kauffman


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)

like image 3
glerendegui Avatar answered Nov 20 '22 00:11

glerendegui


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.

like image 1
derflocki Avatar answered Nov 20 '22 01:11

derflocki