ルート、ユーザー モデル、ユーザー コントローラー、アプリ コントローラー、ページ コントローラーを投稿しました。これは、Web サイトにある 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'));
}
}
私のAppController
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