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).

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

报告相同问题?

悬赏问题

  • ¥28 微信小程序开发页面布局没问题,真机调试的时候页面布局就乱了
  • ¥15 python的qt5界面
  • ¥15 无线电能传输系统MATLAB仿真问题
  • ¥50 如何用脚本实现输入法的热键设置
  • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
  • ¥30 深度学习,前后端连接
  • ¥15 孟德尔随机化结果不一致
  • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀