Archives


Master Symfony2 fundamentals

Be trained by SensioLabs experts (2 to 6 day sessions -- French or English).
trainings.sensiolabs.com

Symfony hosting done right

ServerGrove, outstanding support at the right price for your Symfony hosting needs.
servergrove.com

Discover the SensioLabs Support

Access to the SensioLabs Competency Center for an exclusive and tailor-made support on Symfony
sensiolabs.com

Fabien Potencier
New in Symfony 2.2: New URL Generation Options
by Fabien Potencier – January 14, 2013 – 9 comments

Contributed by
Tobias Schultze
in #3958.

The Routing system is probably one of the most important Component in Symfony. It's a complex beast as it must be flexible, feature-full, and performant at the same time.

Today, let's see a very nice feature that was added to the generator system. I suppose that you all know that Symfony is able to generate two kinds of URLs:

  • absolute URLs: http://example.org/blog/what-a-wonderful-world

    • Twig
      1
      {{ url('blog', { post: 'what-a-wonderful-world' }) }}
      
    • PHP
      1
      2
      $generator->generate('blog', array('post' => 'what-a-wonderful-world'), true);
      $generator->generate('blog', array('post' => 'what-a-wonderful-world'), UrlGeneratorInterface::ABSOLUTE_URL);
      
  • domain-relative URLs (the default): /blog/what-a-wonderful-world

    • Twig
      1
      {{ path('blog', { post: 'what-a-wonderful-world' }) }}
      
    • PHP
      1
      2
      $generator->generate('blog', array('post' => 'what-a-wonderful-world'));
      $generator->generate('blog', array('post' => 'what-a-wonderful-world'), UrlGeneratorInterface::ABSOLUTE_PATH);
      

As of Symfony 2.2 (as of beta2), there are two new kinds of URLs you can generate:

  • schema-relative URLs: //example.org/blog/what-a-wonderful-world

    • Twig
      1
      {{ url('blog', { post: 'what-a-wonderful-world' }, true) }}
      
    • PHP
      1
      $generator->generate('blog', array('post' => 'what-a-wonderful-world'), UrlGeneratorInterface::NETWORK_PATH);
      
  • path-relative URLs: ../ (relative path based on the current URL)

    • Twig
      1
      {{ path('blog', { post: 'what-a-wonderful-world' }, true) }}
      
    • PHP
      1
      $generator->generate('blog', array('post' => 'what-a-wonderful-world'), UrlGeneratorInterface::RELATIVE_PATH);
      

Schema-relative URLs are useful for instance when you need to include a resource hosted from an external website (think jQuery from a CDN) on a website where some pages are accessible via HTTP or HTTPS. Instead of using a piece of JavaScript to switch the protocol depending on the protocol used on the page, and to avoid annoying browser warnings, just use a schema-relative URL:

1
//ajax.googleapis.com/ajax/libs/jquery/1.8.3/jquery.min.js

Path-relative URLs comes in very handy if you need to generate static HTML files that can be downloaded to be browsed locally in a browser (think static blog generator for instance). Let's say that you are on the http://example.org/blog/what-a-wonderful-world page and you want to create a link to the main blog page, which URL is http://example.org/blog/, the relative path URL would be: ./:

 1
 2
 3
 4
 5
 6
 7
 8
 9
10
11
12
13
14
15
16
17
18
19
20
21
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Routing\RouteCollection;
use Symfony\Component\Routing\Route;
use Symfony\Component\Routing\RequestContext;
use Symfony\Component\Routing\Generator\UrlGenerator;

// routes
$routes = new RouteCollection();
$routes->add('page', new Route('/blog/{slug}'));
$routes->add('blog', new Route('/blog/'));

// current context
$request = Request::create('http://example.org/blog/what-a-wonderful-world/comments');
$context = new RequestContext();
$context->fromRequest($request);

// create a generator
$generator = new UrlGenerator($routes, $context);

// generate a relative-path URL (will output ./)
echo $generator->generate('blog', array(), UrlGeneratorInterface::RELATIVE_PATH);

Speaking of a static blog generator, we would probably need a global setting to switch from domain-relative URLs (the default) to path-relative URLs (see ticket #6631) when using the path Twig function in templates.

Comments RSS

  • Grégoire Pineau
    #1 Grégoire Pineau said on the 2013/01/14 at 08:48
    Exemples can be easier to read with named arguments (for the twig part).
  • Marcos Passos
    #2 Marcos Passos said on the 2013/01/14 at 12:37
    Just a tip: using schema relative URLs cause a double download of CSS files in IE 7 & 8, as Steve Souders has pointed out:
    http://www.stevesouders.com/blog/2010/02/10/5a-missing-schema-double-download/
  • Igor Wiedler
    #3 Igor Wiedler said on the 2013/01/14 at 13:37
    The `schema-relative URLs` and `path-relative URLs` seem to be linking to the same code sample.
  • Igor Wiedler
    #4 Igor Wiedler said on the 2013/01/14 at 13:38
    I must say, I agree with Grégoire. Boolean arguments are really not descriptive.
  • Daniel Richter
    #5 Daniel Richter said on the 2013/01/14 at 23:50
    Very nice.

    Next up... object routes? :)
  • Darmen Amanbayev
    #6 Darmen Amanbayev said on the 2013/01/15 at 07:51
    Nice!
  • Tobias Schultze
    #7 Tobias Schultze said on the 2013/01/15 at 10:41
    Fabien, your last example is not correct. It should be `Request::create('http://example.org/blog/what-a-wonderful-world');`. Otherwise the generated reference would be `../` instead of `./`.
  • Tobias Schultze
    #8 Tobias Schultze said on the 2013/01/15 at 10:43
    Also missing a use statement for UrlGeneratorInterface.
  • Beau Simensen
    #9 Beau Simensen said on the 2013/01/16 at 00:31
    Oh this is going to be great! :) Can't wait to work this into Sculpin!