我在CakePHP3.8.13、CakeDC用户8.5.1和PHP7.4上运行了一个CakeDC应用程序。我试图使用自定义控制器,以便可以调整LoginTrait和PasswordChangeTrait的行为。
但是,我的自定义UsersController并没有被使用。
我在/config/bootstrap.php中添加了以下几行
Configure::write('Users.config', ['users']);
Plugin::load('CakeDC/Users', ['routes' => true, 'bootstrap' => true]);我创建了一个新文件/config/users.php
<?php
$config = [
'Users' => [
// Table used to manage users
'table' => 'SomethingThatDoesNotExist',
// Controller used to manage users plugin features & actions
'controller' => 'MyUsers',
// configure Auth component
'auth' => true,
// Password Hasher
'passwordHasher' => '\Cake\Auth\DefaultPasswordHasher',
// token expiration, 1 hour
'Token' => ['expiration' => 3600],
'Email' => [
// determines if the user should include email
'required' => true,
// determines if registration workflow includes email validation
'validate' => true,
],
'Registration' => [
// determines if the register is enabled
'active' => false,
// determines if the reCaptcha is enabled for registration
'reCaptcha' => true,
// allow a logged in user to access the registration form
'allowLoggedIn' => false,
//ensure user is active (confirmed email) to reset his password
'ensureActive' => false,
// default role name used in registration
'defaultRole' => 'user',
],
'reCaptcha' => [
// reCaptcha key goes here
'key' => null,
// reCaptcha secret
'secret' => null,
// use reCaptcha in registration
'registration' => false,
// use reCaptcha in login, valid values are false, true
'login' => false,
],
'Tos' => [
// determines if the user should include tos accepted
'required' => true,
],
'Social' => [
// enable social login
'login' => false,
// enable social login
'authenticator' => 'CakeDC/Users.Social',
],
'GoogleAuthenticator' => [
// enable Google Authenticator
'login' => false,
'issuer' => null,
// The number of digits the resulting codes will be
'digits' => 6,
// The number of seconds a code will be valid
'period' => 30,
// The algorithm used
'algorithm' => 'sha1',
// QR-code provider (more on this later)
'qrcodeprovider' => null,
// Random Number Generator provider (more on this later)
'rngprovider' => null
],
'Profile' => [
// Allow view other users profiles
'viewOthers' => true,
'route' => ['prefix' => false, 'plugin' => 'CakeDC/Users', 'controller' => 'Users', 'action' => 'profile'],
],
'Key' => [
'Session' => [
// session key to store the social auth data
'social' => 'Users.social',
// userId key used in reset password workflow
'resetPasswordUserId' => 'Users.resetPasswordUserId',
],
// form key to store the social auth data
'Form' => [
'social' => 'social'
],
'Data' => [
// data key to store the users email
'email' => 'email',
// data key to store email coming from social networks
'socialEmail' => 'info.email',
// data key to check if the remember me option is enabled
'rememberMe' => 'remember_me',
],
],
// Avatar placeholder
'Avatar' => ['placeholder' => 'CakeDC/Users.avatar_placeholder.png'],
'RememberMe' => [
// configure Remember Me component
'active' => false,
'checked' => true,
'Cookie' => [
'name' => 'remember_me',
'Config' => [
'expires' => '1 month',
'httpOnly' => true,
]
]
],
'Superuser' => ['allowedToChangePasswords' => true],
],
'GoogleAuthenticator' => [
'checker' => \CakeDC\Users\Auth\DefaultTwoFactorAuthenticationChecker::class,
'verifyAction' => [
'plugin' => 'CakeDC/Users',
'controller' => 'Users',
'action' => 'verify',
'prefix' => false,
],
],
'U2f' => [
'enabled' => false,
'checker' => \CakeDC\Users\Auth\DefaultU2fAuthenticationChecker::class,
'startAction' => [
'plugin' => 'CakeDC/Users',
'controller' => 'Users',
'action' => 'u2f',
'prefix' => false,
]
],
];
return $config;注意,指定的表名不是真实的类名,也不存在。应用程序继续运行,没有问题。如果我将配置更改为:
$config = [
'Users' => [
// Table used to manage users
'table' => '\SomethingThatDoesNotExist',
...也就是说,只需将反斜杠添加到我收到的错误表名中即可。
Class '\SomethingThatDoesNotExist' not found. Error in: ROOT\vendor\cakephp\cakephp\src\ORM\Locator\TableLocator.php, line 239我实际上不需要自定义表,我想定制控制器,但无论我输入什么控制器配置,我没有收到任何错误!我在下面的路径/src/Controller/MyUsersController.php,中有一个有效的控制器,但是下面的任何配置都不会触发它的使用:
'controller' => 'MyUsers',
'controller' => '\MyUsers',
'controller' => 'App\Controller\MyUsers',
'controller' => '\App\Controller\MyUsers',如何让我的自定义控制器加载?
发布于 2020-10-12 07:41:32
自定义控制器似乎并不覆盖它仅根据自定义控制器名称覆盖的当前路由,即不是/login,而是/myuser/登录。
https://stackoverflow.com/questions/64259150
复制相似问题