CakePHP w CakeDC users plugin with external database:调用成员函数 user() on boolean
CakePHP w CakeDC users plugin with external database: Call to a member function user() on boolean
我的应用程序是用 CakePHP 3.x 开发的,有 2 个数据库。
我使用 CakedDC Users 插件,它适用于默认数据库中的所有模型。这是我在 AppController 中的内容:
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('RequestHandler', [
'viewClassMap' => [
'docx' => 'Word',
],
]);
$this->loadComponent('Paginator');
$this->loadComponent('CakeDC/Users.UsersAuth');
}
public function beforeFilter(Event $event)
{
$userId = $this->Auth->user('id');
EventManager::instance()->on(new RequestMetadata($this->request, $userId));
$isLogged = $this->Auth->user();
$this->set(compact('isLogged'));
if ($clientId = $this->request->query('client_id')) {
$client = $this->loadModel('Clients')->find()
->where(['id' => $clientId])
->select(['id', 'slug', 'last_name', 'first_name'])
->first();
$this->set(compact('client'));
}
}
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
}
在第二个数据库的控制器中,我有这些功能:
public function initialize()
{
}
public function index()
{
$connection = ConnectionManager::get('db3'); // 'db2' where my second database is configured
$machines = $connection->execute('SELECT * FROM MACHINE');
$this->set(compact('machines'));
$this->set('_serialize', ['machines']);
}
验证后,所有模型都工作正常,除了这个外部视图。我在机器 index.ctp:
中不断收到此错误
这是我得到的错误:
App\Controller\AppController->beforeFilter
CORE\src\Event\EventManager.php, line 422
⟩ Cake\Event\EventManager->_callListener
CORE\src\Event\EventManager.php, line 391
⟩ Cake\Event\EventManager->dispatch
CORE\src\Event\EventDispatcherTrait.php, line 78
⟩ Cake\Controller\Controller->dispatchEvent
CORE\src\Controller\Controller.php, line 497
⟩ Cake\Controller\Controller->startupProcess
CORE\src\Http\ActionDispatcher.php, line 116
⟩ Cake\Http\ActionDispatcher->_invoke
CORE\src\Http\ActionDispatcher.php, line 95
⟩ Cake\Http\ActionDispatcher->dispatch
CORE\src\Routing\Dispatcher.php, line 60
⟩ Cake\Routing\Dispatcher->dispatch
ROOT\webroot\index.php, line 36
Error in: ROOT\src\Controller\AppController.php, line 65
( ! ) Xdebug: user triggered in C:\wamp64\www\inventory\src\Template\Error\error500.ctp on line 33
Call Stack
Time Memory Function Location
1 0.0861 5439240 Cake\Error\BaseErrorHandler->wrapAndHandleException( ) ...\BaseErrorHandler.php:0
2 0.0888 5445696 Cake\Error\BaseErrorHandler->handleException( ) ...\BaseErrorHandler.php:164
3 0.0888 5445696 Cake\Error\ErrorHandler->_displayException( ) ...\BaseErrorHandler.php:180
4 0.0919 5737712 Cake\Error\ExceptionRenderer->render( ) ...\ErrorHandler.php:144
5 0.0932 5878816 Cake\Error\ExceptionRenderer->_outputMessage( ) ...\ExceptionRenderer.php:194
6 0.1235 7711592 Cake\Error\ExceptionRenderer->_outputMessage( ) ...\ExceptionRenderer.php:328
7 0.1235 7711592 Cake\Controller\Controller->render( ) ...\ExceptionRenderer.php:319
8 0.1255 7791592 Cake\View\View->render( ) ...\Controller.php:617
9 0.1260 7792544 Cake\View\View->_render( ) ...\View.php:597
10 0.1261 7793376 Cake\View\View->_evaluate( ) ...\View.php:973
11 0.1263 7822312 include( 'C:\wamp64\www\inventory\src\Template\Error\error500.ctp' ) ...\View.php:1014
12 0.1275 7840560 xdebug_print_function_stack ( ) ...\error500.ctp:33
我应该怎么做?
提前致谢。
您的第二个控制器有一个空 initialize
功能。因此,Auth 组件未加载(由 AppController::initialize
完成,但从未调用),因此当您尝试在 beforeFilter
中使用 $this->Auth
时,它不存在。为什么你的第二个 initialize
在那里?消除它,或者在其中调用 parent::initialize()
,无论哪种方式都会确保加载所需的组件。
我的应用程序是用 CakePHP 3.x 开发的,有 2 个数据库。
我使用 CakedDC Users 插件,它适用于默认数据库中的所有模型。这是我在 AppController 中的内容:
public function initialize()
{
parent::initialize();
$this->loadComponent('Flash');
$this->loadComponent('RequestHandler', [
'viewClassMap' => [
'docx' => 'Word',
],
]);
$this->loadComponent('Paginator');
$this->loadComponent('CakeDC/Users.UsersAuth');
}
public function beforeFilter(Event $event)
{
$userId = $this->Auth->user('id');
EventManager::instance()->on(new RequestMetadata($this->request, $userId));
$isLogged = $this->Auth->user();
$this->set(compact('isLogged'));
if ($clientId = $this->request->query('client_id')) {
$client = $this->loadModel('Clients')->find()
->where(['id' => $clientId])
->select(['id', 'slug', 'last_name', 'first_name'])
->first();
$this->set(compact('client'));
}
}
public function beforeRender(Event $event)
{
if (!array_key_exists('_serialize', $this->viewVars) &&
in_array($this->response->type(), ['application/json', 'application/xml'])
) {
$this->set('_serialize', true);
}
}
在第二个数据库的控制器中,我有这些功能:
public function initialize()
{
}
public function index()
{
$connection = ConnectionManager::get('db3'); // 'db2' where my second database is configured
$machines = $connection->execute('SELECT * FROM MACHINE');
$this->set(compact('machines'));
$this->set('_serialize', ['machines']);
}
验证后,所有模型都工作正常,除了这个外部视图。我在机器 index.ctp:
中不断收到此错误这是我得到的错误:
App\Controller\AppController->beforeFilter CORE\src\Event\EventManager.php, line 422 ⟩ Cake\Event\EventManager->_callListener CORE\src\Event\EventManager.php, line 391 ⟩ Cake\Event\EventManager->dispatch CORE\src\Event\EventDispatcherTrait.php, line 78 ⟩ Cake\Controller\Controller->dispatchEvent CORE\src\Controller\Controller.php, line 497 ⟩ Cake\Controller\Controller->startupProcess CORE\src\Http\ActionDispatcher.php, line 116 ⟩ Cake\Http\ActionDispatcher->_invoke CORE\src\Http\ActionDispatcher.php, line 95 ⟩ Cake\Http\ActionDispatcher->dispatch CORE\src\Routing\Dispatcher.php, line 60 ⟩ Cake\Routing\Dispatcher->dispatch ROOT\webroot\index.php, line 36
Error in: ROOT\src\Controller\AppController.php, line 65 ( ! ) Xdebug: user triggered in C:\wamp64\www\inventory\src\Template\Error\error500.ctp on line 33 Call Stack
Time Memory Function Location
1 0.0861 5439240 Cake\Error\BaseErrorHandler->wrapAndHandleException( ) ...\BaseErrorHandler.php:0 2 0.0888 5445696 Cake\Error\BaseErrorHandler->handleException( ) ...\BaseErrorHandler.php:164 3 0.0888 5445696 Cake\Error\ErrorHandler->_displayException( ) ...\BaseErrorHandler.php:180 4 0.0919 5737712 Cake\Error\ExceptionRenderer->render( ) ...\ErrorHandler.php:144 5 0.0932 5878816 Cake\Error\ExceptionRenderer->_outputMessage( ) ...\ExceptionRenderer.php:194 6 0.1235 7711592 Cake\Error\ExceptionRenderer->_outputMessage( ) ...\ExceptionRenderer.php:328 7 0.1235 7711592 Cake\Controller\Controller->render( ) ...\ExceptionRenderer.php:319 8 0.1255 7791592 Cake\View\View->render( ) ...\Controller.php:617 9 0.1260 7792544 Cake\View\View->_render( ) ...\View.php:597 10 0.1261 7793376 Cake\View\View->_evaluate( ) ...\View.php:973 11 0.1263 7822312 include( 'C:\wamp64\www\inventory\src\Template\Error\error500.ctp' ) ...\View.php:1014 12 0.1275 7840560 xdebug_print_function_stack ( ) ...\error500.ctp:33
我应该怎么做?
提前致谢。
您的第二个控制器有一个空 initialize
功能。因此,Auth 组件未加载(由 AppController::initialize
完成,但从未调用),因此当您尝试在 beforeFilter
中使用 $this->Auth
时,它不存在。为什么你的第二个 initialize
在那里?消除它,或者在其中调用 parent::initialize()
,无论哪种方式都会确保加载所需的组件。