Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Plugin naming: lowercase or camelcase? #7568

Closed
mehov opened this issue Jan 7, 2023 · 2 comments
Closed

Plugin naming: lowercase or camelcase? #7568

mehov opened this issue Jan 7, 2023 · 2 comments

Comments

@mehov
Copy link
Contributor

mehov commented Jan 7, 2023

https://book.cakephp.org/4/en/intro/conventions.html#plugins-conventions:

The convention is to use lowercase letters and dashes as separator

your-name/cakephp-foo-bar

https://book.cakephp.org/4/en/plugins.html#manually-autoloading-plugin-classes:

"MyPlugin\\": "plugins/MyPlugin/src/"

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 be CakePHPExample or CakephpExample.

@mehov
Copy link
Contributor Author

mehov commented Jan 7, 2023

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:

  • rename the plugin folder to CakephpWordpress - to match what it's looking for, but against the lowercase convention (if there is one)
  • edit composer.json to refer to plugins/CakephpWordpress/src
  • change the namespace from CakePress to CakephpWordpress
  • dump autoload and clear the cache again

@mehov
Copy link
Contributor Author

mehov commented Jan 7, 2023

It's the namespace that makes the difference. It has to be CakephpWordpress. With that namespace, the lowercase folder name works too.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant