Published
2/13/2014
Categories
Web Development

How Symfony1.4 Provides a Route to a Better URL

As the web has become ever-present in peoples lives, users are now very familiar with what they commonly call web addresses. Users have also seen them grow in quantity as well as length. The web has of course evolved to provide more user friendly versions.

The Symfony framework does that (and more) for web applications. The following is for Symfony1.4 with an update article for Symfony2 coming soon

Symfony’s Treatment of URLs A web “address” is actually a URL (“Uniform Resource Locator”), a unique entry point for a web application framework. Symfony is a popular PHP web framework choice of many web developers. Symfony applies special treatment to URLs via its routing mechanism. That treatment results in:

User-friendly request URLs that are “pleasing” to the eye Better application security since its internal architecture is not exposed More maintainable application less affected by URL changes Symfony uses the configuration file “routing.yml” to map both the external URL and the internal URI (“Uniform Resource Identifier”). The Symfony internal URI expects a “module” and an “action” and other optional parameters. Consequently the external URL can consist of practically any characters and also be mapped to something else. A traditional URL:

endertech.com/index.php?mode=development&code=php&year=2011

Examples of potential corresponding Symfony URLs:

endertech.com/mode/development/code/php/year/2011

or

endertech.com/AwardWinningDevelopment

Defining routing rules in the routing.yml file is what makes this possible. A basic routing rule consists of a route name, a pattern to match and some optional parameters (additional customization options are available). Example of a route name:

url: “pattern to match , with a default of /” param(optional): { module: mymodule, action: myaction }

In a route pattern, a * (star) matches a collection of variable/value pairs separated by slashes and a word prefixed with a colon : (colon) matches a variable. Example of a simple routing file:

homepage: url: / param: { module: default, action: index }

default_index: url: /:module param: { action: index }

default: url: /:module/:action/*

Request URLs Translation in Symfony When a request is made to a Symfony application, its front controller handles the request and the routing system analyzes the request URL. The routing.yml configuration file is scanned from top to bottom to find a matching route pattern for the request URL. Since the first matching route pattern is used, specific rules should be listed at the top and generic rules toward the bottom of the file. Examples of external URLs and deduced internal URIs:

Request URL Matching Route Name Internal URI endertech.com/ homepage default/index endertech.com/development default_index development/index endertech.com/development/php/year/2011 default development/php?year=2011 While the action occurs, the request parameters hold the module name and action name and any other parameters sent. For the above example “endertech.com/development/php/year/2011” the request parameter “module” will hold “development”, the request parameter “action” will be “php” and the “year” will hold “2011”.

URL Creation via Symfony When URLs are created inside a Symfony application, the inbuilt Symfony helpers, such as “url_for()” and “link_to()” should be used so that an internal URI can be transformed into an external URI. For example, to generate the URL “endertech.com/develop/year/2011” from a Symfony application, the equivalent calls would be:

url_for('develop/php?year=2011')

or

url_for( HYPERLINK "mailto:'@default"'@default?module=develop&action=php&year=2011)

Using route names is fast and preferred since the routing system does not have to check through all the routes. All of the above is only a description of the basic treatment of URLs within the Symfony1.4 framework. It also provides web developers with a multitude of customization options beyond such basics.