登录页面不会使用 CakePHP ACL 重定向


login in page doesn't redirect with CakePHP ACL

我正在遵循CakePHP ACL教程http://book.cakephp.org/2.0/en/tutorials-and-examples/simple-acl-controlled-application/simple-acl-controlled-application.html

我有一个名为ImagesController的控制器,它基本上允许用户列出所有已上传的图像。还有诸如"上传"和"删除"图像之类的操作。

我还有一个具有登录和注销功能的用户控制器。

我的应用控制器.php看起来像这样

  1 <?php
  2 
  3 class AppController extends Controller {
  4     public $components = array(
  5             'Acl',
  6             'Auth' => array(
  7                 'authorize' => array(
  8                     'Actions' => array('actionPath' => 'controllers')
  9                     )
 10                 ),
 11             'Session'
 12             );
 13     public $helpers = array('Html', 'Form', 'Session');
 14 
 15     public function beforeFilter() {
 16  //       $this->Auth->actionPath = 'controllers/';
 17         //Configure AuthComponent
 18         $this->Auth->loginAction = array('controller' => 'users', 'action' => 'login');
 19         $this->Auth->logoutRedirect = array('controller' => 'users', 'action' => 'login');
 20         $this->Auth->loginRedirect = array('controller' => 'images', 'action' => 'index');
 21         $this->Auth->allow('display');
 22  //       $this->Auth->allow('*');
 23     }
 24 }
 25 
 26 
 27 ?>
~             

我的用户控制器.php看起来像这样

<?php
  2 App::uses('AppController', 'Controller');
  3 /**
  4  * Users Controller
  5  *
  6  * @property User $User
  7  */
  8 class UsersController extends AppController {
 30     public function beforeFilter() {
 31         parent::beforeFilter();
 32         //$this->Auth->allow("initDB"); // remove this later
 33         $this->Auth->allow('login', 'logout');
 34     }
 35     public function login() {
 36         if ($this->Session->read('Auth.User')) {
 37             $this->Session->setFlash('You are logged in!');
 38             $this->redirect('/', null, false);
 39         }
 40     }
 41     public function logout() {
 42         //Leave empty for now.
 43         $this->Session->setFlash('Good-Bye');
 44         $this->redirect($this->Auth->logout());
 45     }
}

所以现在当我单击图像/索引中的操作(例如上传或删除)时,它会将我发送到用户/登录页面,但是当我尝试登录时,即使我的重定向指向第 20 行中的图像/索引,也没有任何反应。什么给?

与 Cake 1.3 不同,在 Cake 2 中,登录不会自动完成。这意味着您必须明确地调用$this->Auth->login()

看看教程中的 login() 操作。到目前为止,您从未执行过登录,这解释了为什么您没有被重定向。