私たちの CakePHP アプリケーションでは、ログイン用に Auth コンポーネントを試しました。
これは AppController です。
class AppController extends Controller {
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'homes', 'action' => 'dashboard'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'home')
)
);
public function beforeFilter() {
$this->Auth->allow('index','logout','display','home');
}
これはユーザーコントローラーです:
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add');
}
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 index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('Checkin now'));
$this->redirect(array('action' => 'login'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
}
次に、次の手順に従います。
- ブラウザに入力された URL
http://localhost/cakephp/
- Homepage から、ボタンを使用してログイン
home.ctp
するためにナビゲートしますhttp://localhost/cakephp/users/login
Login
- ユーザー名とパスワードを入力し、ログインボタンをクリックします
home.ctp
次に、 に記載されているページではなく、以前にアクセスしたページにリダイレクトしますAppController
。
2 回目の試行:
- URL
http://localhost/cakephp/users/login/
フィールドから直接アクセス - 次にログイン資格情報を入力すると、 に記載されている正しいページにリダイレクトされ
AppController
ます。
なぜAuthコンポーネントはこのように振る舞うのか.....