一伪货、Laravel路由
寫在routes的web.php
Route::get(‘/’,’[控制器]@[方法]’);
//如下:
Route::get('/posts','\App\Http\Controllers\PostController@index');
1.路由參數
Route::get('/posts/{id}','\App\Http\Controllers\PostController@index');
//在PostController里面
function index($id){
//在PostController中$id就等于上面的
}
2.路由分組
分組前:
Route::get('/posts','\App\Http\Controllers\PostController@index');
Route::get('/posts/create','\App\Http\Controllers\PostController@index');
Route::get('/posts/{id}','\App\Http\Controllers\PostController@index');
分組后:
//參數一:共有屬性
Route::group(['prefix'=>'posts'],function (){
Route::get('/','\App\Http\Controllers\PostController@index');
Route::get('/create','\App\Http\Controllers\PostController@index');
Route::get('/{id}','\App\Http\Controllers\PostController@index');
});
3.綁定模型
綁定前:
Route::get('/posts/{id}','\App\Http\Controllers\PostController@show');
function show($id){
//模型是user,查找模型實體
$user = \App\User::find($id);
//進行操作
}
綁定后:
//默認 user => 表:users => 主鍵:id
Route::get('/posts/{user}','\App\Http\Controllers\PostController@show');
function show(\App\User $user){
//進行操作
}