【问题标题】:CakePHP Error: Class App\Controller\AuthComponent not foundCakePHP 错误:找不到类 App\Controller\AuthComponent
【发布时间】:2016-11-03 01:09:44
【问题描述】:

我正在使用 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

我还想限制对所有控制器和操作的访问,无需登录。这就是为什么AppsController.php 中没有$this-&gt;Auth-&gt;allow()

【问题讨论】:

    标签: authentication cakephp namespaces cakephp-3.2


    【解决方案1】:

    使用 Cake\Controller\Component\AuthComponent;

    【讨论】:

    • ... 和 Controller\Component :)
    猜你喜欢
    • 1970-01-01
    • 2013-06-29
    • 2017-06-09
    • 1970-01-01
    • 2020-04-13
    • 1970-01-01
    • 2018-05-17
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多