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 做个有关计算的小程序
  • ¥15 MPI读取tif文件无法正常给各进程分配路径
  • ¥15 如何用MATLAB实现以下三个公式(有相互嵌套)
  • ¥30 关于#算法#的问题:运用EViews第九版本进行一系列计量经济学的时间数列数据回归分析预测问题 求各位帮我解答一下
  • ¥15 setInterval 页面闪烁,怎么解决
  • ¥15 如何让企业微信机器人实现消息汇总整合
  • ¥50 关于#ui#的问题:做yolov8的ui界面出现的问题
  • ¥15 如何用Python爬取各高校教师公开的教育和工作经历
  • ¥15 TLE9879QXA40 电机驱动
  • ¥20 对于工程问题的非线性数学模型进行线性化