2
votes

I would like to create a plugin for CakePHP 3.1.4. The documentation is straight forward, but the example doesn't work (http://book.cakephp.org/3.0/en/plugins.html#creating-your-own-plugins)

The steps are:

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

Create the database. Connect to the database. Create a table "contacts". Navigate in the directory and run:

bin/cake bake plugin ContactManager

Create the controller:

bin/cake bake controller --plugin ContactManager Contacts

Re-generate the autoloader:

composer dumpautoload

Add this line to the /config/bootstrap.php file:

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

But now, the documentation sais

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

But this doesn't work. Instead I get an error:

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

This means the plugin could not be loaded otherwise it would not suggest this. When opening the DebugKit under "Include" the plugin is not in the plugins array.

I checked the composer.json files and in both the plugin is listed correctly. The bake command ran through without errors. I tried the above steps with multiple new projects with different names.

What is the problem here? Thank you very much.

1
Did you create the plugins/ContactManager/config/routes.php file like the tutorial says?ADmad
@ADmad The bake command for the plugin creates this file already and the contents of the file match the documentation. I just gave it another try.ST2OD
When adding manual routing in /plugins/ContactManager/config/routes.php it seams to work and show the correct template page. Router::connect('/contactmanager/contacts/view/*',['plugin' => 'ContactManager', 'controller' => 'Contacts', 'action' => 'view']); But this should work without adding every route for every action manually, right?ST2OD

1 Answers

3
votes

Finally, I found the solution.

What the docs say should be in /plugins/ContactManager/config/routes.php AND what bake plugin creates:

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

But what really needs to be in the file instead of the above snippet is:

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