CakeDC/Users Call to undefined method App\Controller\CustomUsersController::getUsersTable()

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.

Possibly because you are extending CustomUsers off AppController.

You might need to extend it off \CakeDC\Users\Controller\UsersController
or
include a use CustomUsersTableTrait; statement in your CustomUsersController which will give you the getUsersTable() method that is missing

e.g

class CustomUsersController extends 
    \CakeDC\Users\Controller\UsersController
{
//...

or

use CakeDC\Users\Controller\Traits\LoginTrait;
use CakeDC\Users\Controller\Traits\RegisterTrait;
use CakeDC\Users\Controller\Traits\CustomUsersTableTrait;
class CustomUsersController extends AppController
{
use LoginTrait;
use RegisterTrait;
use CustomUsersTableTrait;

/**
1 Like

Thanks @jmcd73.

Adding the “use CustomUsersTableTrait” as per your suggestion has it working for me now so I’ll move on with that approach.

Much appreciated.

For completeness, I did have it working when I extended off of UsersController but the docs for CakeDC/Users gives the code below as the example for extending the controller so thought there might be a reason for extending off of AppController and wanted to try and get it working in line with the docs.

The code I pasted above didn’t include all the “use” statements from below but it still didn’t work for me even when I copied this code (changing “MyUsersTable” to “CustomUsersTable”) so maybe a documentation error.

namespace App\Controller;

use App\Controller\AppController;
use App\Model\Table\MyUsersTable;
use Cake\Event\Event;
use CakeDC\Users\Controller\Traits\LoginTrait;
use CakeDC\Users\Controller\Traits\RegisterTrait;

class MyUsersController extends AppController
{
use LoginTrait;
use RegisterTrait;

/**
 * Initialize
 *
 * @return void
 */
public function initialize(): void
{
    parent::initialize();
    $this->loadComponent('CakeDC/Users.Setup');
    if ($this->components()->has('Security')) {
        $this->Security->setConfig(
            'unlockedActions',
            [
                'login',
                'webauthn2faRegister',
                'webauthn2faRegisterOptions',
                'webauthn2faAuthenticate',
                'webauthn2faAuthenticateOptions',
            ]
        );
    }
}

//add your new actions, override, etc here

}