gpt4 book ai didi

authentication - CakePHP 错误 : Class App\Controller\AuthComponent not found

转载 作者:行者123 更新时间:2023-12-04 16:54:39 27 4
gpt4 key购买 nike

我正在使用 CakePHP 3.2 并编写一个只有管理员才能登录的管理面板。

有单独的表admins存储管理员凭据。有users表也​​用于用户从主应用程序注册/登录。

我必须使用 admins表登录管理面板。

我所做的是。

<?php
namespace App\Controller;

use Cake\Controller\Controller;
use Cake\Event\Event;


class AppController extends Controller
{

public function initialize()
{
parent::initialize();

$this->loadComponent('RequestHandler');
$this->loadComponent('Flash');
$this->loadComponent('Auth', [
'loginAction' => [
'controller' => 'Admins',
'action' => 'login',
'plugin' => 'Admins'
],
'loginRedirect' => [
'controller' => 'ServiceRequests',
'action' => 'index'
],
'logoutRedirect' => [
'controller' => 'Admins',
'action' => 'login'
],
'authenticate' => [
'Form' => [
'userModel' => 'Admin',
'fields' => [
'username' => 'email',
'password' => 'password'
]
]
]
]);
}

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);
}
}
}
AdminsController.php
<?php
namespace App\Controller;

use App\Controller\AppController;
use Cake\Event\Event;
use App\Controller\AuthComponent;

/**
* Admins Controller
*
* @property \App\Model\Table\AdminsTable $Admins
*/
class AdminsController extends AppController
{
public function beforeFilter(Event $event)
{
parent::beforeFilter($event);
$this->Auth->allow('add');
// Pass settings in using 'all'
$this->Auth->config('authenticate', [
AuthComponent::ALL => ['userModel' => 'Members'],
'Basic',
'Form'
]);
}

public function login()
{
if ($this->request->is('post')) {
$user = $this->Auth->identify();
if ($user) {
$this->Auth->setUser($user);
return $this->redirect($this->Auth->redirectUrl());
}
$this->Flash->error(__('Invalid username or password, try again'));
}
}

public function logout()
{
return $this->redirect($this->Auth->logout());
}
}

但这行不通。并给出 Error: Class App\Controller\AuthComponent' not found
此外,我想限制对所有 Controller 和操作的访问,而无需登录。这就是为什么没有 $this->Auth->allow()AppsController.php

最佳答案

use Cake\Controller\Component\AuthComponent;

关于authentication - CakePHP 错误 : Class App\Controller\AuthComponent not found,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38131564/

27 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com