亚洲在线久爱草,狠狠天天香蕉网,天天搞日日干久草,伊人亚洲日本欧美

為了賬號安全,請及時綁定郵箱和手機立即綁定
已解決430363個問題,去搜搜看,總會有你想問的

路由中的中間件和控制器構造函數中的中間件有什么區別?

路由中的中間件和控制器構造函數中的中間件有什么區別?

PHP
白豬掌柜的 2022-06-11 18:06:34
我對 laravel 很陌生,想深入了解中間件。我想知道將中間件與路由附加或在控制器的構造函數中添加中間件之間的主要區別是什么。例如,它是名為的控制器的構造函數UserControllerpublic function __construct() {  $this->middleware('age');}讓我們假設它是同一個控制器的路由,即UserControllerRoute::get('user/profile', function () {    //})->middleware('age');我的主要問題是,我應該在路由和控制器的構造函數中添加中間件還是只在其中一個中添加中間件?
查看完整描述

2 回答

?
PIPIONE

TA貢獻1829條經驗 獲得超9個贊

兩者都將執行相同的任務,只是您可以用兩種不同的方式編寫。

However, it is more convenient to specify middleware within your controller's constructor. Using the middleware method from your controller's constructor, you may easily assign middleware to the controller's action. You may even restrict the middleware to only certain methods on the controller class.

https://laracasts.com/discuss/channels/laravel/middleware-in-controller-or-on-route


查看完整回答
反對 回復 2022-06-11
?
繁花如伊

TA貢獻2012條經驗 獲得超12個贊

public function __construct() {

  $this->middleware('age');

}

```````````````````````````````````````````````````````````````````````````````

Global middlewares are those that will be running during every HTTP request of your application. In the $middleware property of your app/Http/Kernel.php class, you can list all the global middleware for your project.


```````````````````````````````````````````````````````````````````````

Route::get('user/profile', function () {

    //

})->middleware('age');

````````````````````````````````````````````````````````````````````````

When you want middlewares to specific routes, you have to add the middleware with a key for your app/Http/Kernel.php file and such middlewares are called route middleware. $routeMiddleware by default holds entries for the middleware that are already incorporated in Laravel. For adding your custom middleware, you need to append them to the list and add a key of your choice


[link]https://www.w3schools.in/laravel-tutorial/middleware/  


查看完整回答
反對 回復 2022-06-11
  • 2 回答
  • 0 關注
  • 138 瀏覽

添加回答

舉報

0/150
提交
取消
微信客服

購課補貼
聯系客服咨詢優惠詳情

幫助反饋 APP下載

慕課網APP
您的移動學習伙伴

公眾號

掃描二維碼
關注慕課網微信公眾號