您的位置:首页 > 编程语言 > PHP开发

06. Laravel 4 高级路由

2013-12-20 21:28 477 查看

命名路由

// 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