Introducing Zend Framework 2.0 Router
Ben Scholzen
DASPRiD's

ZendCon Uncon, October 2011

Presenter Notes

What is a route?

  • Matches a request and extracts variables
  • Assembles (in case of HTTP) a URL

Presenter Notes

Route stacks

  • Collection of multiple routes to match/assemble
  • Based on priority stacks
  • Types:
    • SimpleRouteStack
    • HttpTreeRouteStack

Presenter Notes

HTTP specific routes

Presenter Notes

Literal route

  • Matches an literal portion of a string
  • Only provides configured parameters

Presenter Notes

Segment route

  • Works a lot like the original ZF1 route
  • Provides parameter matching based on delimiters and constraints
  • Allows optional segments (both literal and parameter parts)

Presenter Notes

Regex route

  • Matches based on a supplied RegEx pattern
  • Parameter names are determined by named groups
  • Assembling is done with a separate specification

Presenter Notes

Wildcard route

  • Matches a set of /key/value pairs
  • Allows changing the delimiters between key/values as well as delimiters between parameters

Presenter Notes

Part route

  • Base for tree routing
  • Contains one route which has to match to proceed with the child routes

Presenter Notes

Time for some actual code

Presenter Notes

Creating a route stack

Will be used in the following examples:

1 <?php
2 use Zend\Mvc\Router\Http;
3 
4 $router = new Http\TreeRouteStack();

Presenter Notes

Adding a single route to the stack

1 <?php
2 $router->addRoute(
3     $name,
4     $route,
5     $priority = null
6 );

Presenter Notes

Adding multiple routes to the stack

1 <?php
2 $router->addRoutes(array(
3     'route1' => $route1,
4     'route2' => $route2,
5 ));

Presenter Notes

HTTP specific routes

Presenter Notes

Creating a literal route

1 <?php
2 // Will match "/foo"
3 $route = new Http\Literal(
4     '/foo',
5     array('controller' => 'foo')
6 );

Presenter Notes

Creating a regex route

1 <?php
2 // Will match "/" followed by a controller name
3 $route = new Http\Regex(
4     '/(?<controller>[^/]+)',
5     '/%controller%'
6 );

Presenter Notes

Creating a segment route

1 <?php
2 // Will match "/" followed by a controller followed
3 // by an optional action
4 $route = new Http\Segment(
5     '/:controller[/:action]'
6 );

Segment route definitions are actually compiled into regex patterns

Presenter Notes

Changing segment delimiters

1 <?php
2 // Will match "/foo-page1/bar" and "/foo/bar"
3 $route = new Http\Segment(
4     '/:foo{-/}[-page:page]/bar',
5     array('page' => '\d+')
6 );

Presenter Notes

Translating a segment route

1 <?php
2 // In german, it will match "/bier/becks"
3 $route = new Http\Segment(
4     '/{beer}/:{beer-name}'
5 );

Presenter Notes

Creating a wildcard route

1 <?php
2 // Will match an unlimited number of "/key/value" pairs
3 $route = new Http\Wildcard();

Key/Value and parameter delimiters can be changed

1 <?php
2 // Will match an unlimited number of "~key-value" pairs
3 $route = new Http\Wildcard('-', '~');

Presenter Notes

Connecting routes with part routes

 1 <?php
 2 $route = new Http\Part(new Http\Literal(
 3     '/bacon',
 4     array('controller' => 'bacon')
 5 ), true);
 6 
 7 $route->addRoutes(array(
 8     'definition' => new Http\Segment(
 9         '/is/:something'
10     ),
11     'greater' => new Http\Literal(
12         '/greater-than-cookies'
13         array('controller' => 'cookies')
14     ),
15 ));

Presenter Notes

Doing it with configuration

 1 <?php
 2 $router = Http\TreeRouteStack::factory(array(
 3     'routes' => array('bacon' => array(
 4         'type' => 'literal',
 5         'options' => array(
 6             'route' => '/literal',
 7             'defaults' => array(
 8                 'controller' => 'bacon'
 9             )
10         ), 'may_terminate' => true,
11         'child_routes' => array(
12             'definition' => array(
13                 'type'    => 'segment',
14                 'options' => array(
15                     'route' => '/is/:something'
16                 )
17             ),
18         )
19     ))
20 ));

Presenter Notes

Assembling with tree routing

 1 <?php
 2 $url = $router->assemble(
 3     array(
 4         'something' => 'awesome',
 5     ),
 6     array(
 7         'name' => 'bacon/definition'
 8     )
 9 );
10 
11 echo $url; // /bacon/is/awesome

Presenter Notes

Assembling absolute URLs

 1 <?php
 2 $url = $router->assemble(
 3     array(
 4         'something' => 'awesome',
 5     ),
 6     array(
 7         'name'     => 'bacon/definition',
 8         'absolute' => true
 9     )
10 );
11 
12 echo $url; // http://example.com/bacon/is/awesome
  • Great for sending URLs in e-mails
  • Will use current scheme and host based on request

Presenter Notes

Perfomance hints

  • Use tree routing as much as possible
  • Cache the router (serialized with Zend\Cache for instance)

Presenter Notes

Thanks for listening!

  • Rate this talk on joind.in: http://joind.in/4013
  • Twitter handle / IRC nickname: DASPRiD
  • http://www.dasprids.de
  • mail@dasprids.de

Presenter Notes