I am just starting with a CakePHP 5 development and have installed the CakeDC/Users plugin.
I wanted to test extending the plugin and thought I’d followed the instructions and I can access the login page without an issue. However, whenever I attempt to access the “register” page, for which I wanted to use the plugin method, I receive a “Call to undefined method App\Controller\CustomUsersController::getUsersTable()” error, which is coming from the plugin and I can see exists as a method in the UsersController class.
My code is:
Bootstrap method in Application.php
public function bootstrap(): void { // Call parent to load bootstrap from files. parent::bootstrap(); if (PHP_SAPI !== 'cli') { FactoryLocator::add( 'Table', (new TableLocator())->allowFallbackClass(false) ); } Configure::write('DebugKit.ignoreAuthorization', true); $this->addPlugin(Plugin::class); Configure::write('Users.config', ['users']); }
Changes to users.php
$config = [
‘Users’ => [
// Table used to manage users
‘table’ => ‘CustomUsers’,
// Controller used to manage users plugin features & actions
‘controller’ => ‘CustomUsers’,
CustomUsersController
namespace App\Controller;
use CakeDC\Users\Controller\Traits\LoginTrait;
use CakeDC\Users\Controller\Traits\RegisterTrait;class CustomUsersController extends AppController
{
use LoginTrait;
use RegisterTrait;/** * @return void * @throws \Exception */ public function initialize(): void { parent::initialize(); $this->loadComponent('CakeDC/Users.Setup'); if ($this->components()->has('Security')) { $this->Security->setConfig( 'unlockedActions', [ 'login', 'register', ] ); } }
}
plugins.php
return [
// Plugins only needed when in debug mode
‘DebugKit’ => [‘onlyDebug’ => true],// Optional plugins which are only needed in CLI commands 'Bake' => ['onlyCli' => true, 'optional' => true], // Required plugins only in CLI commands 'Migrations' => ['onlyCli' => true], // Add your custom plugins here 'CakeDC/Users' => ['routes' => true, 'bootstrap' => true],
];
Added following to permissions.php
[ 'role' => '*', 'plugin' => null, 'controller' => 'CustomUsers', 'action' => ['register', 'login'], 'bypassAuth' => true, ],
The only difference I can see from what is in the docs is the use of plugins.php to load the plugin instead of explicitly loading it.
Also, I can get it to work when I change CustomUsersController to extend UsersController instead of AppController, which makes sense since the getUserTable method is in the UsersController. Is it an error in the plugin docs as extending the plugin table and entity classes require extending the plugin classes rather than the base Table or Entity class.
Any advice greatly appreciated.