2

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.

ST2OD
  • 705
  • 1
  • 5
  • 15
  • 3
    Did you create the `plugins/ContactManager/config/routes.php` file like the tutorial says? – ADmad Nov 24 '15 at 16:53
  • @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 Nov 25 '15 at 07:17
  • 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 Nov 25 '15 at 10:15

1 Answers1

3

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();
});
ST2OD
  • 705
  • 1
  • 5
  • 15
  • If you are sure that it is wrong in the docs as well as in the bake result I suggest to report it as issue on [GitHub](https://github.com/cakephp/cakephp/issues), so you can help to improve the open source framework :-) – Oops D'oh Dec 01 '15 at 10:58
  • 2
    Above not working for me I found another solution. `Router::plugin('ContactManager', ['path' => '/ContactManager'], function ($routes) { $routes->fallbacks('DashedRoute'); });` – RavatSinh Sisodiya Jun 10 '16 at 10:22