在Laravel中注册中间件很容易:
只需在您的$middleware属性中列出中间件类app/Http/Kernel.php类
或
如果您想将中间件分配给特定的路由,您应该首先在您的app/Http/Kernel.php文件
但是如何在OctoperCMS插件中做到这一点呢?"路由和初始化"是否用于代替Kernel.php
注册中间件?如果没有,插件在哪里可以注册自己的中间件?
如文档中所述,您可以在plugin.php的引导方法中扩展控制器类;
public function boot()
{
// Extend Controller Class
'Cms'Classes'CmsController::extend(function($controller) {
$controller->middleware('Middleware'Path..');
});
// OR Push it directly to the Kernel
// prependMiddleware : Add a new middleware to beginning of the stack.
$this->app['Illuminate'Contracts'Http'Kernel']
->prependMiddleware('Middleware'Path..');
// pushMiddleware : Add a new middleware to end of the stack.
$this->app['Illuminate'Contracts'Http'Kernel']
->pushMiddleware('Middleware'Path..');
}
您也可以将其添加到插件的routes.php
文件中:
Route::group(['prefix' => 'foo'], function () {
Route::get('{slug}', function($slug){
....
})->where('slug', '(.*)?')->middleware('Path'To'Middleware');
});
我在插件routes.php 中使用了"中间件"选项,该选项具有完全限定的类名和路由组
Route::group(['middleware' => ''Namespace'ABC123'AuthMiddleware'], function(){
使用此
Cms'Classes'CmsController::extend(function($controller) {
$controller->middleware('your middleware here');
});
实际上,您可以在插件register
回调上推送中间件,因为它是在中间件发生之前注册的。
class YourPlugin extends PluginBase
{
public function register()
{
$kernel = app()->make('Illuminate'Contracts'Http'Kernel');
$kernel->pushMiddleware(YourMiddleWare::class);
}
}
您必须列出中间件类
$middleware property
Kernel.php类的。