Michael Coury
Michael Coury

Reputation: 191

CakePHP 3 Plugin Routes don't seem to be loading

I'm trying to load routes in a plugin in CakePHP 3.2. They work fine if I put the routes in the core routes.php file, but not in my plugin routes.php file.

The plugin name is: MFC/HDParser.

The path is: /vendor/mfc/hdparser. (The plugin was originally baked into the plugins directory, but transferred it to the vendors directory (and all the files updated) just in case that was the issue.)

In the core bootstrap file I have:

Plugin::load('MFC/HDParser', ['bootstrap' => true, 'routes' => true, 'autoload' => true]);

/vendor/cakephp-plugins contains the line:

'MFC/HDParser' => $baseDir . '/vendor/mfc/hdparser/'

/composer.json contains:

"autoload": {
    "psr-4": {
        "App\\": "src",
        "mfc\\hdparser\\": "./vendor/mfc/hdparser/src",
    }
},

My /vendor/mfc/hdparser/config/routes.php contains:

use Cake\Routing\Router;

Router::plugin(
        'MFC/HDParser', ['path' => '/hdparser'], function ($routes) {
         $routes->connect('/charactersheet', ['plugin' => 'MFC/HDParser', 'controller' => 'Charactersheet', 'action' => 'index']);

//  $routes->connect('/:controller');
//
//$routes->resources('Charactersheet');
//        $routes->fallbacks('DashedRoute');
//}
);
//Router::connect('/charactersheet', ['plugin' => 'MFC/HDParser', 'controller' => 'Charactersheet', 'action' => 'index']);

I've also tried using 'path' => '/mfc/hdparser'

When I try to access site.dev/charactersheet I get "Error: CharactersheetController could not be found."

If I try to use a route like /mfc/hdparser/charactersheet, /MFC/HDParser/charactersheet or /m-f-c..., /m_f_c..., /Mfc..., etc I get a missing controller ('Mfc') error.

If I put the line:

Router::connect('/charactersheet', ['plugin' => 'MFC/HDParser', 'controller' => 'Charactersheet', 'action' => 'index']);

in the core routes.php file (outside the scope, before Plugins:load(); or inside the scope using $routes->connect() ) it works fine.

I've tried inflecting just about everything using underscores, dashes and camelcase.

I've dug through the documentation (http://book.cakephp.org/3.0/en/plugins.html, http://book.cakephp.org/3.0/en/development/routing.html, http://api.cakephp.org/3.2/class-Cake.Routing.Router.html) and tried everything I could find there (and here), as well as using the CakeDC/Users plugin as a template, but I'm still missing something...

--MFC

Upvotes: 0

Views: 1525

Answers (1)

Roel
Roel

Reputation: 326

I had the same issue with CakePHP 3.3, the routes.php file in my plugin was not loaded.

I fixed it by putting Plugin::routes(); after loading my plugins. This method loads all routes for plugins, where the configuration has 'routes' => true.

E.g.

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

Upvotes: 2

Related Questions