发布于: 2022-05-25 17:04:46

先看看laravel自带的控制器方法名称

Route::resource('photos', 'PhotoController');

我们就可以看到routes路由列表里注册了这些路由


方法uri路由名称控制器@方法
GETphotosphotos.indexPhotoController@index
POSTphotosphotos.storePhotoController@store
GETphotos/createphotos.createPhotoController@create
GETphotos/{photo}photos.showPhotoController@show
PUT/PATCHphotos/{photo}photos.updatePhotoController@update
DELETEphotos/{photo}photos.destroyPhotoController@destroy
GETphotos/{photo}/editphotos.editPhotoController@edit


* GET /photos
index()  //  展示照片列表

* POST /photos
store()  // 添加照片

* GET /photos/create
create()  // 展示用来创建照片的表单

* GET /photos/{id}
show($id)  // 展示一张照片

* PUT /photos/{id}
update($id)  // 更新一张照片

* DELETE /photos/{id}
destroy($id)  // 移除一张照片

* GET /photos/{id}/edit
edit($id)  // 展示编辑照片表单

基础路由

//闭包路由
Route::get('foo', function () {return 'Hello World';});
//控制器路由
Route::get('/index.html', [IndexController::class, 'index']);
Route::get('/link.html', [IndexController::class, 'link']);
Route::resource('articles',"Web\ArticleController" )->middleware('throttle:30,1');//访问频次每分钟最多访问30次
Route::resource('books','Web\BookController');

//前台默认首页
Route::get('/', function () { return view('index');}); 
Route::get('/index.html', function () { return redirect('/', 301); });
Route::get('/index.htm', function () { return redirect('/', 301); });
Route::get('/index', function () { return redirect('/', 301); });

Route::get('/news-cate{cateid}/article-{id}.html','NewsController@show')->where('id', '[0-9]+');
//有中间件的后台路由 比如判断登录 isLogin 判断权限 hasRule
Route::group(['prefix'=>'admin','namespace'=>'Admin','middleware'=>['isLogin','hasRule']],function(){
   //后台订单管理
   Route::resource('order','OrderController');
   //后台账号管理
   Route::resource('member','MemberController');
  //
   Route::resource('goods','GoodsController');
});

# 对参数局部约束
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]+']);

Route::any($uri, $callback);        // 任意 method
Route::match(['get', 'post'], '/', function () {
    //
});

注意点 在 web.php 路由里的 POST, PUT, DELETE 方法,在提交表单时候必须加上CSRF参数。

<form method="POST" action="/profile">
    @csrf
    ...</form>
表单伪造
<input type="hidden" name="_method" value="PUT">
// 或者 @method('PUT')



一些简化的基本路由

# 重定向路由
Route::redirect('/here', '/there');
Route::permanentRedirect('/here', '/there');  // 301
Route::redirect('/here', '/there', 301);  // 第三个参数不写则默认为 302

# 只需要返回一个视图
Route::view('/welcome', 'welcome');
Route::view('/welcome', 'welcome', ['name' => 'Taylor']);

最后兜底的路由

Route::fallback(function () {
   // 处理 404
});  // 一定要放在所有路由最后面

获取当前路由信息

// 假设有路由: Route::get('/', 'TestController@test')->name("mytest");

$route = Route::current(); // 返回  object(Illuminate\Routing\Route)
$name = Route::currentRouteName(); // 返回 mytest

$action = Route::currentRouteAction(); // 控制器中返回:App\Http\Controllers\TestController@test  闭包中返回:null
路由前缀
Route::prefix('admin')->group(function () {
   Route::get('users', function () {

   });
});
// 相当于
Route::get('admin/users', function () {

});
路由命名前缀
Route::name('admin.')->group(function () {
   Route::get('users', function () {

   })->name('users');  // name('admin.users')
});
添加中间件
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 () {
       // 使用 `first` 和 `second` 中间件
   });

   Route::get('user/profile', function () {
       // 使用 `first` 和 `second` 中间件
   });
});

模型


# 自定义键名,在模型中修改(默认指的是数据库中的主键 id):
# App/User.php
public function getRouteKeyName()
{
   return 'slug';
   // api/users/2
   // select * from `users` where `slug` = 2 limit 1
}

延伸阅读
    发表评论