简体   繁体   中英

Laravel 5.3 middleware callback function in controller constructor

as mentioned in the documentation of Laravel 5.3 i am trying to add a middleware callback in the constructor of a controller

Controller's also allow you to register middleware using a Closure. This provides a convenient way to define a middleware for a single controller without defining an entire middleware class:

$this->middleware(function ($request, $next) {
    // ...
    return $next($request);
});

unfortunately i am getting the error:

Illegal offset type

which is logical because the middleware method of the Illuminate\\Routing\\Controller class accepts a the first param as an array only, i am not confused why would they add this code sample to the documentation although its not gonna work, or am I doing something wrong or missing something ?

documentation page

the laravel version should be at least:

5.3.4

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM