基本路由
您的應用程式的絕大多數路由將在 app/routes.php 檔案中定義。 Laravel 中最簡單的路由由一個 URI 和一個閉包呼叫組成。
基本GET 路由
<span style="font-size: 16px;">Route::get('/', function()<br/>{<br/>return 'Hello World';<br/>});<br/></span>
基本POST 路由
<span style="font-size: 16px;">Route::post('foo/bar', function()<br/>{<br/>return 'Hello World';<br/>});<br/></span>
註冊一個路由以響應所有HTTP 方法
<span style="font-size: 16px;">Route::any('foo', function()<br/>{<br/> return 'Hello World';<br/>});<br/></span>
強制一個路由必須通過HTTPS 訪問
<span style="font-size: 16px;">Route::get('foo', array('https', function()<br/>{<br/> return 'Must be over HTTPS';<br/>}));<br/></span>
經常您需要根據路由產生URLs,您可以透過使用URL::to 方法:
<span style="font-size: 16px;">$url = URL::to('foo');<br/></span>
路由參數
<span style="font-size: 16px;">Route::get('user/{id}', function($id)<br/>{<br/>return 'User '.$id;<br/>});<br/></span>
的可選路由參數
<span style="font-size: 16px;">Route::get('user/{name?}', function($name = null)<br/>{<br/>return $name;<br/>});<br/></span>
帶預設值的可選的路由參數
<span style="font-size: 16px;">Route::get('user/{name?}', function($name = 'John')<br/>{<br/>return $name;<br/>});<br/></span>
帶正規表示式約束的路由
<span style="font-size: 16px;">Route::get('user/{name}', function($name)<br/>{<br/>//<br/>})<br/>->where('name', '[A-Za-z]+');<br/>Route::get('user/{id}', function($id)<br/>{<br/>//<br/>})<br/>->where('id', '[0-9]+');<br/></span>
Route::filter('old', function() { if (Input::get('age') < 200) { return Redirect::to('home'); } });
Route::get('user', array('before' => 'old', function() { return 'You are over 200 years old!'; }));
Route::get('user', array('before' => 'auth|old', function() { return 'You are authenticated and over 200 years old!'; }));
Route::filter('age', function($route, $request, $value) { // }); Route::get('user', array('before' => 'age:200', function() { return 'Hello World'; }));
Route::filter('log', function($route, $request, $response, $value) { // });
Route::filter('admin', function() { // }); Route::when('admin/*', 'admin');
Route::when('admin/*', 'admin', array('post'));
class FooFilter { public function filter() { // Filter logic... } }
Route::filter('foo', 'FooFilter');
Route::get('user/profile', array('as' => 'profile', function() { // }));
Route::get('user/profile', array('as' => 'profile', 'uses' => 'UserController@showProfile'));
$url = URL::route('profile'); $redirect = Redirect::route('profile');
$name = Route::currentRouteName();
Route::group(array('before' => 'auth'), function() { Route::get('/', function() { // Has Auth Filter }); Route::get('user/profile', function() { // Has Auth Filter }); });
Route::group(array('domain' => '{account}.myapp.com'), function() { Route::get('user/{id}', function($account, $id) { // }); });
Route::group(array('prefix' => 'admin'), function() { Route::get('user', function() { // }); });
Route::model('user', 'User');
Route::get('profile/{user}', function(User $user) { // });
Route::model('user', 'User', function() { throw new NotFoundException; });
Route::bind('user', function($value, $route) { return User::where('name', $value)->first(); });
更多Laravel框架路由配置總結、設定技巧大全相關文章請關注PHP中文網!