Skip to content

Instantly share code, notes, and snippets.

@graste
Last active August 29, 2015 14:10
Show Gist options
  • Save graste/f46ed1aeb776ae6b698c to your computer and use it in GitHub Desktop.
Save graste/f46ed1aeb776ae6b698c to your computer and use it in GitHub Desktop.
Route and URL generation in PHP frameworks

Route and URL generation in PHP frameworks

// Symfony\Component\Routing\Generator\UrlGeneratorInterface
generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH);
// Symfony\Component\Routing\Router
generate($name, $parameters = array(), $referenceType = self::ABSOLUTE_PATH)
// Symfony\Bundle\FrameworkBundle\Controller\Controller
generateUrl($route, $parameters = array(), $referenceType = UrlGeneratorInterface::ABSOLUTE_PATH)
  • Silex wraps the Symfony Components UrlGenerator, so generate($name, $parameters) works and is accompanied by functions from a Twig extension: path() and url() - those methods are available on the $app as well.
  • Laravel - multiple ways via UrlGenerator::to and many helper methods on different classes
// UrlGenerator
to($path, $extra = array(), $secure = null)
route($name, $parameters = array(), $absolute = true, $route = null)
  • Drupal8 - Symfony based, so surprisingly:
UrlGenerator::generate($name, $parameters = array(), $absolute = FALSE)
  • Agavi
// AgaviRouting (plus AgaviWebRouting, AgaviWebserviceRouting, AgaviConsoleRouting, AgaviSoapRouting…)
gen($route, array $params = array(), $options = array())
// There's an AgaviIRoutingValue interface that extends ArrayAccess and allows to specify name, value, prefix, postfix and if a routing value needs encoding
  • CakePHP - some weird stuff, but basically strings and nested array structures. The default Route implementation has the following constructor:
__construct($template, $defaults = array(), $options = array())
  • Codeigniter - I got lost in their source and gave up quickly, seems to be strings and arrays and helpers and http_build_query
  • Zeta Components - some lookup and query parameters adding via func_get_args() usage, basically this:
UrlCreator::getUrl($name, $queryparams…)
  • ZendFramework2 - just concat strings, call http_build_query or use some helpers they provide for such things
// https://github.com/zendframework/zf2/blob/master/library/Zend/View/Helper/Url.php
__invoke($name = null, $params = array(), $options = array(), $reuseMatchedParams = false)
createUrl($route, $params = array(), $ampersand = '&')
<?php
interface UrlGeneratorInterface
{
/**
* Generates a URL for the given route name and parameters. The options
* MAY be used to provide hints for the URL generation. This includes
* information about different parts of a URL, e.g. the scheme or port to
* use or whether absolute or relative URLs should be generated.
*
* The usual URL has the following simplified structure:
*
* scheme://userinfo@host:port/path?query#fragment
*
* @see http://en.wikipedia.org/wiki/URI_scheme
* @see http://tools.ietf.org/html/rfc3986
* @see http://tools.ietf.org/html/std66
*
* Suggested option keys and their default values are:
*
* - 'relative': false
* whether to generate absolute or relative URLs
* - 'separator': '&'
* query parameters separator, e.g. ';' – see ini_get('arg_separator.output')
* - 'scheme': null
* scheme name, e.g. 'ftp' – true/false to include/exclude;
* use an empty string ('') to generate protocol relative URLs ('//host.tld')
* - 'userinfo': null
* user information string, e.g. 'user:pwd' – true/false to include/exclude
* - 'host': null
* host string, e.g 'example.org' – true/false to include/exclude
* - 'port': null
* port string, e.g. '8080' – true/false to include/exclude
* - 'authority': null
* authority string, e.g. 'user:info@host:port' – true/false to include/exclude
* - 'path': null
* path string, e.g. 'some/hierarchical/path' – true/false to include/exclude
* - 'fragment': null
* fragment identifier string, e.g. 'foo' – true/false to include/exclude
* - 'use_trans_sid': false
* whether or not to include a session id (SID) – see ini_get('session.use_trans_sid')
*
* Other options may be used as well. Adapters for frameworks should
* use and convert those options to their respective implementations of
* handling the generation of URLs.
*
* While this interface is for primarily for generating URLs, it SHOULD be
* possible to create any URIs depending on the implementations and the
* provided options.
*
* @param string $name route name or lookup key to generate an URL for
* @param array $parameters pairs of placeholder names and values
* @param array $options array of options to influence URL generation
*
* @return string URL relative or absolute URL
*/
public function generateUrl($name, array $parameters = [], array $options = []);
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment