配置: app/config
UTC
');database.default
',
'Mysql' );
路由:app/routes.php
基本 GET 路由
Route::get('/', function()
{
return 'Hello World';
});
基本 POST 路由
Route::post('foo/bar', function()
{
return 'Hello World';
});
註冊一個路由以響應所有 HTTP 方法
Route::any('foo', function()
{
return 'Hello World';
});
強制一個路由必須通過 HTTPS 訪問
Route::get('foo', array('https', function()
{
return 'Must be over HTTPS';
}));
經常您需要根據路由產生 URLs,您可以通過使用 URL::to 方法:
$url = URL::to('foo');
路由參數:
Route::get('user/{id}', function($id)
{
return 'User '.$id;
});
可選參數:
Route::get('user/{name?}', function($name = null)
{
return $name;
});
正則表達式匹配:
Route::get('user/{name}', function($name)
{
//
})
->where('name', '[A-Za-z]+');
Route::get('user/{id}', function($id)
{
//
})
->where('id', '[0-9]+');
命名路由:
命名路由在更易於在生成跳轉或 URLs 時指定路由。您可以像這樣爲路由指定一個名字:
Route::get('user/profile', array('as' => 'profile', function()
{
//
}));
您也可以爲控制器的方法指定路由名字:
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));
現在您在生成 URLs 或跳轉的時候使用路由的名字:
$url = URL::route('profile');
$redirect = Redirect::route('profile');
您可以使用 currentRouteName
方法獲取一個路由的名字:
$name = Route::currentRouteName()
子域名路由:
Route::group(array('domain' => '{account}.myapp.com'), function()
{
Route::get('user/{id}', function($account, $id)
{
//
});
});
404錯誤:
有兩種方法在路由中手動觸發一個404錯誤。首先,您可以使用 App::abort
方法:
App::abort(404);
其次,您可以拋出一個 Symfony\Component\HttpKernel\Exception\NotFoundHttpException
的實例。
// 404 頁面
App::missing(function($exception)
{
return Response::view('errors.missing', array(), 404);
});