cakephp3加载特殊前缀的插件 [英] cakephp3 load plugin for special prefix

查看:75
本文介绍了cakephp3加载特殊前缀的插件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有Cakephp 3应用程序,我想加载此插件及其下面的事件监听器,仅用于管理前缀. (不是整个项目).

I have Cakephp 3 application and i want to load this plugin and the event listener below it just for admin prefix. ( not the entire project ) .

我该如何实现?

这是我的

config/bootstrap.php

config/bootstrap.php

我在哪里加载此插件. (我将它们加载到文件末尾)

where i load this plugin . (I load these at end of the file )

...

Plugin::load('Josegonzalez/Upload');

\Cake\Event\EventManager::instance()->on(new \App\Controller\Event\AppEventListener());

我希望这些仅在管理员前缀上运行

i want these to line only run on admin prefix

推荐答案

引用cakephp插件文档 https://book.cakephp.org/3.0/en/plugins.html#plugin-routes

Quoting cakephp plugin docs https://book.cakephp.org/3.0/en/plugins.html#plugin-routes

您还可以在应用程序的路由列表中加载插件路由.这样做可以让您更好地控制插件路由的加载方式,并可以将插件路由包装在其他作用域或前缀中.

You can also load plugin routes in your application’s routes list. Doing this provides you more control on how plugin routes are loaded and allows you to wrap plugin routes in additional scopes or prefixes.

所以你可以做

  Router::scope('/', function ($routes) {
       // Connect other routes.
        $routes->scope('/admin', function ($routes) {
        $routes->loadPlugin('Josegonzalez/Upload');
       });
       });

对于您的事件监听器,将其加载到插件引导程序配置中

For your event listener, load it inside plugin bootstrap configuration

    // inside plugin bootstra.php
    \Cake\Event\EventManager::instance()->on(new 
     \App\Controller\Event\AppEventListener());

这篇关于cakephp3加载特殊前缀的插件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆