2 回答

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

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/
- 2 回答
- 0 關注
- 138 瀏覽
添加回答
舉報