基础路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
Route::get('foo', function(){ return 'hello world'; });
Route::get('/user', 'UserController@index');
Route::get('posts/{postid}', function($postid){ });
|
参数
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34
| Route::get('posts/{postid}/comment/{commnetid}', function($pid, $cid){ });
Route::get('users/{name?}', function($name='Json'){ return $name; });
Route::get('user/{id}', function($id){ })->where('id', '[0-9]+');
Route::get('user/{id}/{name}', function($id,$name){ })->where(['id' => '[0-9]+'], 'name'=>'[a-z]+');
public function boot(){
Route::patter('id', '[0-9]+'); parent::boot(); }
Route::get('user/{id}', function(){ });
|
参数绑定
隐式绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16
|
Route::get('api/users/{user}', function(App\User $user){ return $user->email; });
public function getRouteKeyName(){ return 'slug'; }
|
显示绑定
1 2 3 4 5 6 7 8 9 10 11 12 13 14
|
public function boot(){ parent::boot();
Route::model('user', App\User::class); }
Route::get('profile/{user}', function($user){ })
|
自定义解析逻辑
1 2 3 4 5 6 7 8 9 10
|
public function boot(){ parent::boot();
Route::bind('user', function($user){ return App\User::where('name', $value)->first() ?? abort(404); }); }
|
1 2 3 4 5 6 7
| 在 app/User.php中
public function resolverRouteBinding($value){ return $this->where('name', $value)->first() ?? abort(404); }
|
http 请求方法
单个方法
1 2 3 4 5 6 7 8 9 10
| Route::get($uri, $callback); Route::post($uri, $callback); Route::put($uri, $callback); Route::patch($uri, $callback); Route::delete($uri, $callback); Route::options($uri, $callback);
Route::resource('photos', 'PhotoController');
|
方法 |
uri |
路由名称 |
控制器@方法 |
Get |
photos |
photos.index |
PhotoController@index |
Post |
photos |
photos.store |
PhotoController@store |
Get |
photos/create |
photos.create |
PhotoController@create |
Get |
photos/{photo} |
photos.show |
PhotoController@show |
Put/PATCH |
photos/{photo} |
photos.update |
PhotoController@update |
Delete |
photos/{photo} |
photos.destory |
PhotoController@destory |
Get |
photos/{photo}/edit |
photos.edit |
PhotoController@edit |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21
| * Get /photos index()
* Post /photos store()
* Get /photos/create create()
* Get /photos/{id} show($id)
* Put /photos/{id} update($id)
* Delete /photos/{id} destory($id)
* Get /photos/{id}/edit
|
组合
1 2 3 4 5 6 7
| Route::any($uri, $callback);
Route::match(['get','post'], '/ ', function(){ })
|
注意: web路由里的POST,PUT,DELETE方法,在提交表单时必须加上CSRF参数。
表单伪造
1 2 3 4
| <input type="hidden" name="_method" value="PUT" /> // 或者 @method('PUT')
|
命名路由
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20
| Route::get('user/profile', function (){ })->name('profile');
$url = route('profile'); return redirect()->route('profile');
Route::get('user/{id}/profile', function($id){ })->name('profile');
$url = route('profile', ['id' => 1]);
|
根据需求丰富路由
命名空间
1 2 3 4 5 6 7
| Route::namespace('Admin')->get('/user', 'UserController@index');
ROute::namespace('Admin')->get('user2', 'UserController@user2')
|
子域名路由
1 2 3 4 5 6 7
| Route::domain('{account}.myapp.com')->group(function(){ Route::get('user/{id}', function($account, $id){ }) });
|
路由前缀
1 2 3 4 5 6 7 8 9 10 11 12
| Route::prefix('admin')->group(function(){ Route::get('ser', function(){ }) })
Route::get('admin/users', function(){ });
|
路由命名前缀
1 2 3 4 5 6
| Route::name('admin.')->group(function(){ Route::get('users', function(){ })->name('users'); });
|
添加中间件
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25
| Route::middleware('throttle:60,1')->group(function(){
Route::get('/user', function(){ }) })
Route::get('/user', function(){ })->middleware('auth:api');
Route::middleware(['first,'second'])->group(function(){
Route::get('/', function(){ // });
Route::get('user/profile', function(){ // }) })
|
简化路由
1 2 3 4 5 6 7 8 9 10
|
Route::redirect('/here', '/there'); Route::permanentRedirect('/here', '/there'); Route::redirect('/here', '/there', 301);
Route::view('/welcome', 'welcome'); Route::view('/welcome', 'welcome', ['name' => 'Taylor']);
|
默认路由
1 2 3 4 5 6
|
Route::fallback(function(){ })
|
获取当前路由信息
1 2 3 4 5 6 7 8 9
|
$route = Route::current(); $name = Route::currentRouteName();
$action = Route::currentRouteAction();
|