0

我已经发布了我的路线、用户模型、用户控制器、应用程序控制器和页面控制器。这几乎是他们网站上针对 CakePHP 2 的简单身份验证指南的完整副本。

我正在使用 2.2.2,但似乎无法正常工作。我遇到的主要问题是,许多指南/帮助假设您正在将其构建为一个新的“基础”站点,也就是说,Cake 已安装到主机的根目录。但在我的情况下,它不是,我将它作为展示的一部分来展示我能做什么。

这样做的问题是它只重新加载主页(登录表单所在的页面)。它不会接受有效的用户 + 密码并对其进行任何操作,无效的详细信息也是如此。所以我在做一些基本的错误?或者因为尝试在同一个域中托管许多不同的项目/站点的配置,我需要在某个地方进行更改吗?

我没有发布我在获取用户名+密码时使用的表单的代码,但我没有从指南发布的内容中更改它。

如果您需要其他我可能没想过要发布的内容,请告诉我。

请帮我。

我的路线配置

$SiteBase = '/projects/cake/DrWho/';

Router::connect($SiteBase, array('controller' => 'pages', 'action' => 'display'));
Router::connect($SiteBase . 'login/', array('controller' => 'users', 'action' => 'LogIn'));

Router::connect($SiteBase . 'gallery/', array('controller' => 'galleries', 'action' => 'index'));
Router::connect($SiteBase . 'episodeguide/', array('controller' => 'episodes', 'action' => 'index'));
Router::connect($SiteBase . 'forum/', array('controller' => 'forumtopics', 'action' => 'index'));

//Router::connect('/pages/*', array('controller' => 'pages', 'action' => 'display'));

CakePlugin::routes();
require CAKE . 'Config' . DS. 'routes.php';

我的用户模型

App::uses('AppModel', 'Model');
App::uses('AuthComponent', 'Controller/Component');
class User extends AppModel {

  public function beforeSave($options = array()) {
    if (isset($this->data[$this->alias]['password'])) {
    $this->data[$this->alias]['password'] = AuthComponent::password($this->data[$this->alias]['password']);
    }
    return true;
  }

  public $validate = array(
    'id' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'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
        ),
    ),
    'username' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'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' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'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
        ),
    ),
    'fristname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'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
        ),
    ),
    'surname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'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
        ),
    ),
    'email' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'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
        ),
    ),
    'active' => array(
        'boolean' => array(
            'rule' => array('boolean'),
            //'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
        ),
    ),
    'role_id' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'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
        ),
    ),
  );

  public $belongsTo = array(
    'Role' => array(
        'className' => 'Role',
        'foreignKey' => 'role_id',
        'conditions' => '',
        'fields' => '',
        'order' => ''
    )
  );
}

我的用户控制器

App::uses('AppController', 'Controller');
class UsersController extends AppController {

public function login() {
if ($this->request->is('post')) {
    if ($this->Auth->login()) {
    $this->redirect($this->Auth->redirect());
    } else {
    $this->Session->setFlash(__('Invalid username or password, try again'));
    }
}
}   

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

public function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('add', 'logout');
}

public function index() {
    $this->User->recursive = 0;
    $this->set('users', $this->paginate());
}

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));
}

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.'));
        }
    }
}

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);
        unset($this->request->data['User']['password']);
    }
}

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'));
  }

}

我的应用控制器

App::uses('Controller', 'Controller');

class AppController extends Controller {
  public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'pages', 'action' => 'index'),
        'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home')
    )
  );
  var $helpers = array('AssetCompress.AssetCompress');

  function beforeFilter() {

  if ($this->request->is('mobile')) {
    $this->isMobile = true;
    $this->set('isMobile', true );
    $this->autoRender = false;
    $this->layout = 'mobile';
  }   
  $this->MenuSystem();

  } //End of beforeFilter function

  function MenuSystem() {
  $this->loadModel('Menu');
  $MenuSQL = $this->Menu->find('all', array('conditions' => array('active' => true)));
  $this->set('Menu', $MenuSQL);
 } //End of MenuSystem function

} //End of AppController Class

我的页面控制器

 App::uses('AppController', 'Controller');

 class PagesController extends AppController {

  public $name = 'Pages';
  public $uses = array();

  function beforeFilter() {
    parent::beforeFilter();
    $this->Auth->allow('*');
  }

  function index() {
    $this->render('pages_home');
  }

  public function display() {
    if ($this->request->is('mobile')) {
    $this->render('pages_mobile');
    } else {
    $this->render('pages_home');
    }
  } //End display() function

    } // End of PagesController Class
4

1 回答 1

0

我认为您的蛋糕项目位于文件夹内DOCUMENT_ROOT/projects/cake/DrWho/(其中 DOCUMENT_ROOT 是您的网络服务器的文档根目录)。

您不必做任何特别的事情来使蛋糕项目在文档根目录的子文件夹中工作。尝试将 $siteBase 前缀添加到您的路线实际上是导致问题的原因。基本 url 由 cake 自动处理。在基本 url 没有被正确猜到的极少数情况下,您可以App.baseUrl使用app/Config/core.php.

于 2012-10-21T06:38:01.833 回答