#27
もしかしてうわさの無限リダイレクトってやつか?
ログイン画面をデバッグする(CakePHP修行 #012) | IDEA*IDEA
なるほどー
よくわかりました。
ということで認証チェックコール形式へコード修正しました。
app_controller.php
<?php class AppController extends Controller { var $needAuth=false;//ログインを必須とするかどうかのフラグ function checkSession(){ // If the session info hasn't been set... if (!$this->Session->check('auth')){ // Force the user to login $this->redirect('/users/login'); return; } $this->set("auth",$this->Session->read('auth')); } /* function beforeFilter(){ //セッションから取り出したログイン情報をセット $auth=$this->Session->read('auth'); $this->set("auth",$auth); //ログイン必須の機能でログインされていない場合はログイン画面に転送 if($this->needAuth){ if(empty($auth)){ $this->redirect("/users/login"); return; } } } */ } ?>
users_controller.php
<?php class UsersController extends AppController { var $name = 'Users'; var $uses = array('User','Seminar'); // var $needAuth=true;//認証を必須とする。 function index (){ $this->checkSession();//認証チェック $auth=$this->Session->read('auth'); $this->set('me', $this->User->findById($auth['id']));//これだけで関連するseminar情報も取得できるらしい } function login(){ $this->pageTitle = 'Welcome to Papazy!'; $this->set('error', false); if (!empty($this->data)){ $cond=array( 'email'=>$this->data['User']['email'], 'pwd'=>$this->data['User']['pwd'] ); $data=$this->User->findAll($cond); if(count($data)==0){//NG $this->log("LOGINエラー",LOG_ERROR); $this->set('error', true); } else{//OK //セッションにログイン情報を格納する。 $this->Session->write('auth', $data[0]['User']); $this->log("LOGINユーザー:".$data[0]['User']['email'],LOG_DEBUG); $this->flash("{$data[0]['User']['name']}さん、こんにちは。","/users/index/"); } /* $someone = $this->User->findByEmail($this->data['User']['email']); if(!empty($someone['User']['pwd']) && $someone['User']['pwd'] == $this->data['User']['pwd']){ $this->Session->write('my_id', $someone['User']['id']); $this->log("LOGINユーザー:".$this->data['User']['email'],LOG_DEBUG); $this->redirect('/users/index/'); } else{ $this->log("LOGINエラー",LOG_ERROR); $this->set('error', true); } */ } } function logout(){ $this->Session->delete('auth'); $this->flash("さようなら。","/users/login/"); } } ?>