一、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){
//进行操作
}