2015-02-07 32 views
2

我遵循CakePHP提供的ACL教程Simple Acl controlled Application但我刚遇到问题。在我完成教程后,我登录了,但它只是将我重定向到登录页面。请帮忙。这是我的AppController。CakePHP 2.0.0我遵循ACL后无法登录教程

<?php 

class AppController extends Controller { 
public $components = array(
    'Acl', 
    'Auth' => array(
     'authorize' => array(
      'Actions' => array('actionPath' => 'controllers') 
     ) 
    ), 
    'Session' 
); 

public $helpers = array('Html', 'Form', 'Session'); 

public function beforeFilter() { 
    //Configure AuthComponent 
    $this->Auth->allow('display'); 
    $this->Auth->loginAction = array(
     'controller' => 'users', 
     'action' => 'login' 
    ); 
    $this->Auth->logoutRedirect = array(
     'controller' => 'users', 
     'action' => 'login' 
    ); 
    $this->Auth->loginRedirect = array(
     'controller' => 'posts', 
     'action' => 'add' 
    ); 
} 
} 
?> 

这是我UserController.php

<?php 
App::uses('AppController', 'Controller'); 
/** 
* Users Controller 
* 
* @property User $User 
*/ 
class UsersController extends AppController { 



public function beforeFilter() { 
    parent::beforeFilter(); 
    $this->Auth->allow('initDB'); // We can remove this line after we're finished 
} 

public function initDB() { 
    $group = $this->User->Group; 

    // Allow admins to everything 
    $group->id = 1; 
    $this->Acl->allow($group, 'controllers'); 

    // allow managers to posts and widgets 
    $group->id = 2; 
    $this->Acl->deny($group, 'controllers'); 
    $this->Acl->allow($group, 'controllers/Attendees'); 
    $this->Acl->allow($group, 'controllers/Orders'); 


    // allow basic users to log out 
    $this->Acl->allow($group, 'controllers/users/logout'); 
} 
/** 
* index method 
* 
* @return void 
*/ 
public function index() { 
    $this->User->recursive = 0; 
    $this->set('users', $this->paginate()); 
} 
/** 
* view method 
* 
* @param string $id 
* @return void 
*/ 
public function view($id = null) { 
    $this->User->id = $id; 
    if (!$this->User->exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    $this->set('user', $this->User->read(null, $id)); 
} 
/** 
* add method 
* 
* @return void 
*/ 
public function add() { 
    if ($this->request->is('post')) { 
     $this->User->create(); 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 
    } 
    $groups = $this->User->Group->find('list'); 
    $this->set(compact('groups')); 
} 
/** 
* edit method 
* 
* @param string $id 
* @return void 
*/ 
public function edit($id = null) { 
    $this->User->id = $id; 
    if (!$this->User->exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    if ($this->request->is('post') || $this->request->is('put')) { 
     if ($this->User->save($this->request->data)) { 
      $this->Session->setFlash(__('The user has been saved')); 
      $this->redirect(array('action' => 'index')); 
     } else { 
      $this->Session->setFlash(__('The user could not be saved. Please, try again.')); 
     } 
    } else { 
     $this->request->data = $this->User->read(null, $id); 
    } 
    $groups = $this->User->Group->find('list'); 
    $this->set(compact('groups')); 
} 
/** 
* delete method 
* 
* @param string $id 
* @return void 
*/ 
public function delete($id = null) { 
    if (!$this->request->is('post')) { 
     throw new MethodNotAllowedException(); 
    } 
    $this->User->id = $id; 
    if (!$this->User->exists()) { 
     throw new NotFoundException(__('Invalid user')); 
    } 
    if ($this->User->delete()) { 
     $this->Session->setFlash(__('User deleted')); 
     $this->redirect(array('action'=>'index')); 
    } 
    $this->Session->setFlash(__('User was not deleted')); 
    $this->redirect(array('action' => 'index')); 
} 


public function login() { 
    if ($this->Session->read('Auth.User')) { 
     $this->Session->setFlash('You are logged in!'); 
     return $this->redirect('/'); 
    } 
} 

public function logout() { 
    $this->Session->setFlash('Good-Bye'); 
    $this->redirect($this->Auth->logout()); 
} 
} 
+0

任何人都可以帮我吗?请。 – nosnevetzy 2015-02-07 04:45:02

+0

嗨登录问题是什么? – 2015-02-07 04:49:57

+0

登录功能不起作用。当我点击登录按钮时,页面只是重新加载。它没有重定向loginRedirect函数应该是什么。 – nosnevetzy 2015-02-07 04:55:49

回答

0

清洁的登录()动作/方法你的代码,并使用

public function login() { 

if ($this->request->is('post')) { 
     if ($this->Auth->login()) { 
      return $this->redirect($this->Auth->redirectUrl()); 
      // Prior to 2.3 use `return $this->redirect($this->Auth->redirect());` 
     } else { 
      $this->Session->setFlash(__('Username or password is incorrect'), 'default', array(), 'auth'); 
     } 
    } 
} 
+0

我收到一个错误。致命错误:调用未定义的方法AuthComponent :: redirectUrl() – nosnevetzy 2015-02-07 05:18:02

+0

可能是一些与版本有关的问题,你可以使用 - >'return $ this-> redirect(array('controller'=>'posts','action'= >'add')); ' – 2015-02-07 05:30:06

+0

反正它只是一个常数没有任何功能,所以你可以设置'返回$ this->重定向(array('controller'=>'posts','action'=>'add'));'没有任何问题。 – 2015-02-07 05:33:52