douyanyan1123 2016-07-15 08:49
浏览 587
已采纳

我是否需要从Laravel控制器中间件中排除所有非路由操作?

I'm using controller middlewares in my Laravel 5.2 app. According to the docs, to exclude specific actions from being handled by a middleware, I need to use the except array:

class UserController extends Controller
{
    public function __construct() {

        // Exclude foo- and barAction from auth middleware
        $this->middleware('auth', ['except' => [
            'fooAction',
            'barAction',
        ]]);
    }
}

Of course, the total number of methods in a controller will almost always be greater than the number of methods linked to specific routes in routes.php. So except for the route-actions there will be others, dealing strictly with the logic - public or private methods.

Do I need to exclude all those non-route actions from a middleware or excluding the route-actions is enough?

EDIT:

I would say that the other, non-route methods - as they are not accessible from outside - don't need to be excluded from a middeware. The question is rather: is the middleware ran for them every time they are accessed? I wouldn't say so but it's nice to make sure.

  • 写回答

1条回答 默认 最新

  • dongtong2021 2016-07-15 10:22
    关注

    Your $this->middleware() method is defined in Illuminate\Routing\Controller: it merely saves its arguments in a protected property that is only accessed through a getter.

    Other than in tests, that getter is only invoked in two places:

    1. Illuminate\Foundation\Console\RouteListCommand (which handles the route:list Artisan command); and

    2. Illuminate\Routing\ControllerDispatcher (which dispatches routing calls to a controller).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 有没有帮写代码做实验仿真的
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥30 vmware exsi重置后登不上
  • ¥15 c++头文件不能识别CDialog
  • ¥15 Excel发现不可读取的内容
  • ¥15 关于#stm32#的问题:CANOpen的PDO同步传输问题
  • ¥20 yolov5自定义Prune报错,如何解决?
  • ¥15 电磁场的matlab仿真