我遇到问题,当我添加'授权' =>我的app控制器中的数组('控制器'),每次按下编辑或添加或登录它都会转到以下地址:
本地主机/ cakefolder / cakefolder
我收到此错误:
错误:找不到cakefolderController。
但是当我删除'授权' =>来自appController的数组('控制器'),一切正常
AppController.php
<?php
class AppController extends Controller {
public $helpers = array('Html', 'Session', 'Form' );
public $components = array(
'DebugKit.Toolbar',
'Session',
'Auth' => array(
'authorize' => array('Controller'),
'authenticate' => array(
'Form' => array(
'passwordHasher' => 'Blowfish',
'loginRedirect'=>array('Controller'=>'user', 'action'=>'index'),
'logoutRedirect'=>array('Controller'=>'user', 'action'=>'index'),
'authError'=>"you are not allowed to access that page",
)
)
)
);
public function beforeFilter() {
$this->Auth->allow('index', 'add');
$this->set('logged_in', $this->Auth->loggedIn());
$this->set('current_user', $this->Auth->user());
}
}
UserController.php
<?php
App::uses('AppController', 'Controller');
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
// This is to let user edit and delete only their own information
public function isAuthorized($user) {
if (in_array($this->action, array('edit','delete'))) {
if ($user['id'] != $this->request->params['pass'][0]) {
return false;
}
return true;
}
}
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirect());
}
$this->Session->setFlash(__('Invalid username or password, try again'));
}
}
public function logout() {
$this->Auth->logout();
$this->redirect('index');
}
public $components = array('Paginator', 'Session');
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
public function view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
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.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
public function edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
public function delete($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->request->allowMethod('post', 'delete');
if ($this->User->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
} else {
$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
public function full_index() {
$this->User->recursive = 0;
$this->set('users', $this->Paginator->paginate());
}
public function full_view($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
$options = array('conditions' => array('User.' . $this->User->primaryKey => $id));
$this->set('user', $this->User->find('first', $options));
}
public function full_add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
public function full_edit($id = null) {
if (!$this->User->exists($id)) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is(array('post', 'put'))) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved.'));
return $this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$options = array('conditions' => array('User.' . $this->User- >primaryKey => $id));
$this->request->data = $this->User->find('first', $options);
}
}
public function full_delete($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->request->allowMethod('post', 'delete');
if ($this->User->delete()) {
$this->Session->setFlash(__('The user has been deleted.'));
} else {
$this->Session->setFlash(__('The user could not be deleted. Please, try again.'));
}
return $this->redirect(array('action' => 'index'));
}
}
user.php的
<?php
App::uses('AppModel', 'Model', 'Security', 'Utility');
App::uses('BlowfishPasswordHasher', 'Controller/Component/Auth');
class User extends AppModel {
// hash password before saving It
public function beforeSave($options = array()) {
// if ID is not set, we're inserting a new user as opposed to updating
if (!$this->id) {
$passwordHasher = new BlowfishPasswordHasher();
$this->data[$this->alias]['password'] = $passwordHasher->hash($this->data[$this- >alias]['password']);
}
return true;
}
public $primaryKey = 'user_id';
public $displayField = 'username';
public $validate = array(
//USERNAME VALIDATION
'username' => array(
'required' => array(
'rule' => array('minLength', 1),
'allowEmpty' => false,
'message' => 'Please enter a title.'
)
),
'username' => array(
'required' => array(
'rule' => array( 'isUnique' ),
'message' => 'Username already exist. Please try again',
//'allowEmpty' => false,
//'required' => TRUE,
//'last' => TRUE, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
//EMAIL ADDRESS VALIDATION
'email_address' => array(
'required' => array(
'rule' => array('minLength', 1),
'allowEmpty' => false,
'message' => 'Please add an email'
)
),
'email_address' => array(
'required' => array(
'rule' => array( 'isUnique' ),
'message' => 'Email already exist in our database. Please try again',
//'allowEmpty' => false,
//'required' => TRUE,
//'last' => TRUE, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
),
/*'email_address' => array(
'required' => array(
'rule' => array( 'email' ),
'message' => 'Please add a correct email',
//'allowEmpty' => false,
//'required' => TRUE,
//'last' => TRUE, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
),
), */
//PASSWORD VALIDATION
/* 'password' => array(
'minLength' => array(
'rule' => array('minLength', 6),
'message' => 'Your password must be at least 6 characters long.'
),
'notempty' => array(
'rule' => 'notEmpty',
'message' => 'Please fill in the required field.'
)
),
'password_confirmation' => array(
'identical' => array(
'rule' => array('matchPasswords'),
'message' => 'Password confirmation does not match password.'
), */
'password'=>array(
'not empty' => array(
'rule'=>'notEmpty',
'Message'=>'Password is empty'
),
'Match Passwords'=> array(
'rule'=>'matchPasswords',
'message'=>'Password do not match'
)
),
'password_confirmation'=>array(
'not empty' => array(
'rule'=>'notEmpty',
'Message'=>'verify password'
)
)
/* 'user_id' => array(
'alphaNumeric' => array(
'rule' => array('alphaNumeric'),
//'message' => 'Your custom message here',
//'allowEmpty' => false,
//'required' => false,
//'last' => false, // Stop validation after this rule
//'on' => 'create', // Limit validation to 'create' or 'update' operations
), */
);
// PASSWORD CONFIRMATION VALIDATION FUNCTION
public function matchPasswords($data){
if ($data['password'] == $this->data['User']['password_confirmation']) {
return True;
}
$this->invalidate('password_confirmation', 'Your password do not match');
return FALSE;
}
}
答案 0 :(得分:0)
尝试在c
和controller
设置的loginRedirect
中使用小写logoutRedirect
。每当通过数组构建URL时,通常在键中使用小写。
其次,您的基本网址设置可能存在问题。 Auth
组件识别您需要进行身份验证,因此它会尝试将您重定向到users/index
,这可能恰好是/
的默认路由器。但是,它不会转到http://localhost/cakefolder
或http://localhost/cakefolder/users/index
,而是转到http://localhost/cakefolder/cakefolder
。
您可以确认文档根目录的URL吗?并检查您的设置以获取baseUrl的值。