Skip to content

Instantly share code, notes, and snippets.

@h4cc
Created July 30, 2013 13:15
Show Gist options
  • Save h4cc/6112787 to your computer and use it in GitHub Desktop.
Save h4cc/6112787 to your computer and use it in GitHub Desktop.
How to add a _url default to routes, so a external url can be generated by name.
parameters:
router.options.generator_class: "Acme\\DemoBundle\\Routing\\Generator\\UrlGenerator"
router.options.generator_base_class: "Acme\\DemoBundle\\Routing\\Generator\\UrlGenerator"
# This will generate a url to google.
to_google:
path: /to-google
defaults:
_url: "http://www.google.com/"
<?php
namespace Acme\DemoBundle\Routing\Generator;
use Symfony\Component\Routing\Generator\UrlGenerator as BaseUrlGenerator;
class UrlGenerator extends BaseUrlGenerator
{
protected function doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens)
{
if(isset($defaults['_url'])) {
return $defaults['_url'];
}
return parent::doGenerate($variables, $defaults, $requirements, $tokens, $parameters, $name, $referenceType, $hostTokens);
}
}
@h4cc
Copy link
Author

h4cc commented May 24, 2018

@yceruto LOL, this was 5 (!) years ago :) that did not work back than.

@githublirui
Copy link

hi,Is this problem solved?

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment