无法创建简单的 CakePHP 插件

Can't create simple CakePHP plugin

我想为 CakePHP 3.1.4 创建一个插件。文档很简单,但示例不起作用 (http://book.cakephp.org/3.0/en/plugins.html#creating-your-own-plugins)

步骤是:

composer create-project --prefer-dist cakephp/app sampleapp

创建数据库。连接到数据库。创建一个 table "contacts"。在目录中导航 运行:

bin/cake bake plugin ContactManager

创建控制器:

bin/cake bake controller --plugin ContactManager Contacts

重新生成自动加载器:

composer dumpautoload

将此行添加到 /config/bootstrap.php 文件:

Plugin::load('ContactManager', ['routes' => true]);

但是现在,文档说

"If you want to access what we’ve got going thus far, visit /contact-manager/contacts. You should get a “Missing Model” error because we don’t have a Contact model defined yet."

但这不起作用。相反,我得到一个错误:

Missing Controller. Cake\Routing\Exception\MissingControllerException. Cake\Routing\Dispatcher->dispatch ROOT/webroot/index.php, line 37 Error: ContactManagerController could not be found. Error: Create the class ContactManagerController below in file: src/Controller/ContactManagerController.php

这意味着插件无法加载,否则不会提示这个。在 "Include" 下打开 DebugKit 时,插件不在插件数组中。

我检查了 composer.json 文件,两个插件都正确列出。烘焙命令 运行 顺利通过。我用多个不同名称的新项目尝试了上述步骤。

这里有什么问题?非常感谢。

终于找到了解决方法。

文档所说的应该在 /plugins/ContactManager/config/routes.phpbake plugin 中创建的内容:

Router::plugin('ContactManager', function ($routes) {
    $routes->fallbacks('DashedRoute');
});

但文件中真正需要的是:

Router::scope('/contactmanager', ['plugin' => 'ContactManager'], function ($routes) {
    $routes->fallbacks();
});