Skip to content
Aaron Lord edited this page May 12, 2013 · 3 revisions

Filename: app/routes.php

GET Route
    <?php
    Route::get('/', function ()
    {
        return 'Hello world!';
    });

    // or..

    Route::get('/', 'HomeController@index');

POST Route

    <?php
    Route::post( .. );

PUT Route

    <?php
    Route::put( .. )

DELETE Route

    <?php
    Route::delete( .. )

Any HTTP Route

    <?php
    Route::any( .. );

Parameters

Parameters are segments of url that are dynamic. Any that are defined are passed through to the route action in the order that they appear in the url.

Basic Parameter

    <?php
    Route::get('/user/{id}', function ($id)
    {
        return 'User id: '.$id;
    });

Regex Constraints

    <?php
    Route::get('/user/{id}, function ()
    {
        return 'User id: '.$id;
    })
    ->where(array(
        'id' => '[0-9]+'
    ));

Aliases

You may give your routes aliases to make referring to them more convenient.

Attaching an Alias to a Route

    <?php
    Route::get('/', 'HomeController@index')->alias('home');

Filters

Filters provide a convenient method of limiting access to given routes. a filter to be applied before and after the routing takes place by using the before and after methods. sums it up

Returning anything from a filter (other than null) will be considered the routes response and halt execution of the rest of the route.

Defining a Filter

    <?php
    Route::filter('auth', function ()
    {
        if (!User::authed())
        {
            return Redirect::to('home')->now();
        }
    });

Attaching a Filter to a Route

    <?php
    Route::get( .. )->before('auth');

Attaching Multiple Filters to a Route

    <?php
    Route::get( .. )->after(array('auth', 'locale'));

Groups

Groups provide a convenient method of applying multiple routes to multiple routes at once.

Defining a Group

    <?php
    Route::group(function ()
    {
        Route::get( .. );
        Route::post( .. );
        // and more..
    });

Attaching Filters to a Group

    <?php
    Route::group( .. )
    ->before('greet')
    ->after('farewell');

Prefixed routes

You may prefix a group of routes with a segment of the url.

Prefix Routes

    <?php
    Route::group(function ()
    {
        Route::get('/{$country}', function ($country)
        {
            // http://mysite.com/location/uk
            // $country == 'uk'
        });
    })
    ->prefix('/location');

Host Routing

Hosts provide a special way to filter your routes by hostname, providing powerful ways to have multi-domain sites and dynamic subdomains with wildcard DNS settings.

Attaching a Subdomain to a Group

    <?php
    Route::group(function ()
    {
        Route::get('/{$country}', function ($account, $country)
        {
            // http://aaron.mysite.com/location/uk
            // $account == 'aaron'
            // $country == 'uk'
        });
    })
    ->host('{account}.mysite.com');

Regex Constraints

    <?php
    Route::group( .. )
    ->host('{account}.mysite.com')
    ->where(array(
        'account' => '[A-z]+'
    ));