06. Laravel 4 高级路由

命名路由

// app/routes.php
Route::get('/my/long/calendar/route', array(
    'as' => 'calendar',
    function()
    {
        return View::make('calendar');
    }
));

模板中生成指向此路由的链接:

// app/views/example.blade.php
{{ route('calendar') }}

若需获取当前路由的名称,可以使用 $current = Route::currentRouteName(); 方法。

安全路由(HTTPS)

// app/routes.php
Route::get('secret/content', array(
    'https',
    function ()
    {
        return 'Secret squirrel!';
    }
));

路由参数过滤

// app/routes.php
Route::get('save/{princess}/{unicorn}', function($princess, $unicorn)
{
    return "{$princess} loves {$unicorn}";
})
->where('princess', '[A-Za-z]+')
->where('unicorn',  '[0-9]+');

分组路由

// app/routes.php
Route::group(array('before' => 'onlybrogrammers'), function()
{
    // First Route
    Route::get('/first', function()
    {
        return 'Dude!';
    });
    // Second Route
    Route::get('/second', function()
    {
        return 'Duuuuude!';
    });
});

路由前缀

// app/routes.php
Route::group(array('prefix' => 'books'), function()
{
    // First Route
    Route::get('/first', function()
    {
        return 'The Colour of Magic';
    });
    // Second Route
    Route::get('/second', function()
    {
        return 'Reaper Man';
    });
});

如此,访问链接将变成 /books/first/books/second

域名路由

// app/routes.php
Route::group(array('domain' => '{user}.myapp.dev'), function()
{
    Route::get('profile/{page}', function($user, $page)
    {
        // ...
    });
});

如此,访问链接将变成 http://taylor.myapp.dev/profile/1 ,而此时取得的参数:$user = 'taylor'; $page = 1;

你可能感兴趣的:(laravel,route)