Servage Magazine

Information about YOUR hosting company – where we give you a clear picture of what we think and do!

URL routing in Laravel

Friday, February 27th, 2015 by Servage

url-seoCreating good and meaningful URLs is an important part of every web project. You need to consider the logical structure of your URLs as well as their SEO impact. At the same time URLs should follow a specific structure and provide a meaningful aid for the user in understanding what to expect in return from the server for a given endpoint.

Structuring theĀ URL

The old way of creating URLs for dynamic PHP-based sites was to route everything through a single index.php file, with corresponding parameters. An example of such an URL could be:

http://mydomain.com/index.php?page_id=123&option_a=1&option_b=0&session_id=xyz

The example above holds a few values and a session id. Nowadays the session identifier would likely be parsed through a cookie value, thus not being a part of the URL. The other parameter values should be broken into folder-like path-elements, for better readability, like below:

http://mydomain.com/123/1/0

The example above does not even mention the parameter names “page_id”, “option_a” and “option_b”. That is because the URL parser should know the placement of the parameter values, thus eliminating the need to declare their names in addition to the values. However, for search engine optimization you may consider using more explanatory values. Say the page id is for a product list, and the options are for some sorting and filtering. This would look nicer in the following way:

http://mydomain.com/products/2014/sort-by-price

The last example has moved all the way from an URL that isĀ impossible to understand, to a very easily understandable URL. Users would immediately know what to expect from the page that will be returned, thus making this URL much more useful when displayed to potential visitors via search engines etc.

Larvel routes

In Laravel you can structure you URLs to meet any criteria mentioned above. The goal of routing in Laravel is to map a URL to a given code endpoint. This could for instance be a simple callback function:

Route::any('/', function()
{
    return 'Your code';
});

HTTP methods

Notice how you can choose which HTTP methods a given route should map to. This is useful to restrict certain methods, or respond to them very differently.

Route::get('/', function()
{
    return 'Your code - but only for GET method requests';
});

Route parameters

You can add dynamic parameter values to your routes using curly brackets. The values will be passed on for later use in your code.

Route::get('products/{id}', function($id)
{
    return 'Product '.$id;
});

Route prefix

You can group routes if you have many sub-routes with a given common prefix. This makes it easier for settings to be shared among multiple routes, and keeps the route configuration file cleaner.

Route::group(['prefix' => 'products'], function()
{
    Route::get('list', function()
    {
        // Your code here
    });
});

Route subdomains

You can also attach routes to subdomains if necessary:

Route::group(['domain' => '{sub}.myapp.com'], function()
{
    Route::get('products/{id}', function($sub, $id)
    {
        // Your code here
    });
});

Routing generally provides a powerful way of configuring how your application responds to static and dynamic URLs. Understanding routing is fundamental to building your application. In a later article it will be described how you can map routes to controller actions, thus maintaining a better separation between your route configuration and application code.

 

URL routing in Laravel, 5.0 out of 5 based on 2 ratings
Categories: Guides & Tutorials

Keywords: ,

You can follow any responses to this entry through the RSS 2.0 feed. You can leave a response, or trackback from your own site.

No comments yet (leave a comment)

You are welcome to initiate a conversation about this blog entry.

Leave a comment

You must be logged in to post a comment.