Closed mehov closed 1 year ago
Still investigating.
So initially, I followed the former and put the plugin into cakephp-wordpress
folder, so lowercase dash-separated.
Defined it in composer.json
as
"autoload": { "psr-4": { "App\": "src/", "CakePress\": "plugins/cakephp-wordpress/src" } },
Created plugins/cakephp-wordpress/src/Plugin.php
:
<?php
namespace CakePress;
class Plugin extends \Cake\Core\BasePlugin
{
public function bootstrap(\Cake\Core\PluginApplicationInterface $app): void
{
// Add constants, load configuration defaults.
// By default will load `config/bootstrap.php` in the plugin.
parent::bootstrap($app);
}
public function routes($routes): void
{
// Add routes.
// By default will load `config/routes.php` in the plugin.
parent::routes($routes);
}
}
Loaded it in my src/Application.php
:
$this->addPlugin(\CakePress\Plugin::class, array(
'bootstrap' => true,
'routes' => true,
));
Created plugins/cakephp-wordpress/routes.php
with:
<?php
use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;
return static function (RouteBuilder $routes) {
$routes->plugin('cakephp-wordpress', function (RouteBuilder $routes) {
// Routes connected here are prefixed with '/debug-kit' and
// have the plugin route element set to 'DebugKit'.
//$routes->connect('/{controller}');
$routes->connect('/', [
'controller' => 'Posts',
'action' => 'index',
]);
});
};
Made sure the route appears in Debug Kit under Routes:
cakephp-wordpress.posts:index | /cakephp-wordpress | { "controller": "Posts", "action": "index", "plugin": "cakephp-wordpress" }
Finally, got ready to test:
composer dump-autoload && bin/cake cache clear_all
When I went to example.com/cakephp-wordpress
, I got a plain white page that says An Internal Server Error Occurred
. This is not a red CakePHP error page, doesn't have the Debug Kit on it - just a plain white error page.
Here are the entries from logs/error.log
:
Request URL: /cakephp-wordpress/ error: [Cake\Http\Exception\MissingControllerException] Controller class Posts could not be found. in /var/www/app/vendor/cakephp/cakephp/src/Controller/ControllerFactory.php on line 348 Exception Attributes: array ( 'class' => 'Posts', 'plugin' => 'CakephpWordpress', 'prefix' => NULL, '_ext' => NULL, )
Request URL: /cakephp-wordpress/ error: [Cake\Core\Exception\MissingPluginException] Plugin CakephpWordpress could not be found. in /var/www/app/vendor/cakephp/cakephp/src/Core/PluginCollection.php on line 143 Exception Attributes: array ( 'plugin' => 'CakephpWordpress', )
So apparently, it takes the plugin name from within the route. And because the route is cakephp-wordpress
, it looks for the plugin CakephpWordpress
. But the plugin is still cakephp-wordpress
.
However, everything works as soon as I:
CakephpWordpress
- to match what it's looking for, but against the lowercase convention (if there is one)composer.json
to refer to plugins/CakephpWordpress/src
CakePress
to CakephpWordpress
It's the namespace that makes the difference. It has to be CakephpWordpress
. With that namespace, the lowercase folder name works too.
https://book.cakephp.org/4/en/intro/conventions.html#plugins-conventions:
https://book.cakephp.org/4/en/plugins.html#manually-autoloading-plugin-classes:
I'm working on a plugin now, referred to the docs and got confused.
It's also unclear if for a plugin named
cakephp-example
the namespace would beCakePHPExample
orCakephpExample
.