###質問内容
CakePHPのAuth認証について、
コントローラー及びテンプレートファイルに知っている限りの記述をしたのですが、
上手く機能しません。
下記に挙げるコードファイルを前提にフォームからユーザーネームとパスワードをloginメソッドに送ると
**Call to a member function identify() on boolean **
というエラーが出ます。
Auth認証について正しい記述の仕方がわかる方がいればご指摘よろしくお願いします。
###前提
・データベースには、Usersテーブルにid(int型、オートインクリメント、プライマリー)、username(varchar型(30))、password(varchar型(30))、role(varchar型(30))が設定してあります。
・indexでフォームを用意して、login、logoutに一時飛んで、またindexにリダイレクトする形にしています。
・ログイン成功、ログイン済み、ログアウト、のメッセージはセッションコンポーネントで出力して、ログイン失敗のみフラッシュでメッセージ出力しています。できれば、全てフラッシュメッセージにしたかったですが、ここではセッションも使っています。
・UsersController.php
<?php namespace App\Controller; class UsersController extends AppController { public function initialize(){ $this->name = 'Users'; $this->viewBuilder()->autoLayout(true); $this->viewBuilder()->layout('users'); } public function isAuthorized($user = null){ if(in_array($action, ['index','logout','login'])){ return true; } return false; } public $components = ['Session']; public function index(){ } public function login(){ if($this->request->isPost()){ $user = $this->Auth->identify(); if(!empty($user)){ $this->Auth->setUser($user); $this->request->session()->write('two.flag','zumi'); $this->request->session()->write('two.status','ok'); return $this->redirect($this->Auth->login()); } $this->Flash->error('IDまたはパスワードが間違っています。'); } } public function logout(){ $this->request->session()->destroy(); $this->request->session()->write('one.hoge','out'); return $this->redirect($this->Auth->logout()); } } }
・AppController.php
<?php /** * CakePHP(tm) : Rapid Development Framework (http://cakephp.org) * Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * * Licensed under The MIT License * For full copyright and license information, please see the LICENSE.txt * Redistributions of files must retain the above copyright notice. * * @copyright Copyright (c) Cake Software Foundation, Inc. (http://cakefoundation.org) * @link http://cakephp.org CakePHP(tm) Project * @since 0.2.9 * @license http://www.opensource.org/licenses/mit-license.php MIT License */ namespace App\Controller; use Cake\Controller\Controller; use Cake\Event\Event; /** * Application Controller * * Add your application-wide methods in the class below, your controllers * will inherit them. * * @link http://book.cakephp.org/3.0/en/controllers.html#the-app-controller */ class AppController extends Controller { /** * Initialization hook method. * * Use this method to add common initialization code like loading components. * * e.g. `$this->loadComponent('Security');` * * @return void */ public function initialize(){ parent::initialize(); $this->loadComponent('RequestHandler'); $this->loadComponent('Flash'); $this->loadComponent('Auth',[ 'authorize' => ['Controller'], 'authenticate' => [ 'Form' => [ 'fields' => [ 'username' => 'username', 'password' => 'password' ] ] ], 'loginRedirect' => [ 'controller' => 'Users', 'action' => 'index' ], 'logoutRedirect' => [ 'controller' => 'Users', 'action' => 'index' ], 'authError' => 'Failed!', ]); /* * Enable the following components for recommended CakePHP security settings. * see http://book.cakephp.org/3.0/en/controllers/components/security.html */ //$this->loadComponent('Security'); //$this->loadComponent('Csrf'); } /** * Before render callback. * * @param \Cake\Event\Event $event The beforeRender event. * @return \Cake\Network\Response|null|void */ public function beforeRender(Event $event) { if (!array_key_exists('_serialize', $this->viewVars) && in_array($this->response->type(), ['application/json', 'application/xml']) ) { $this->set('_serialize', true); } } }
・テンプレートindex.ctp
<?php $flag = 0; $status = 0; $hoge = 0; if($this->request->session()->check('two.flag')){ $flag = $this->request->session()->read('two.flag'); $status = $this->request->session()->consume('two.status'); }elseif($this->request->session()->check('one.hoge')){ $hoge = $this->request->session()->consume('one.hoge'); } ?> <div class="content"> <?php if($status != 0){ ?> <label>ログイン成功</label> <br> <?=$this->Form->create(null, ['class'=>'log','url'=>['controller'=>'Users', 'action'=>'logout']]) ?> <label> <?=$this->Form->submit("ログアウト",['class'=>'ppp']) ?> </label> <?=$this->Form->end(); ?> <?php }elseif($flag != 0){ ?> <label>ログイン済み</label> <?=$this->Form->create(null, ['class'=>'log','url'=>['controller'=>'Users', 'action'=>'logout']]) ?> <label> <?=$this->Form->submit("ログアウト",['class'=>'ppp']) ?> </label> <?=$this->Form->end(); ?> <?php }else{ ?> <label><?=$this->Flash->render('auth') ?> <?php if($hoge != 0){ echo"ログアウトしました。"; } ?> </label> <?=$this->Form->create(null,['url'=>['controller'=>'Users','action'=>'login']]) ?> <?=$this->Form->text("username",['class'=>'ppp','name'=>'username','placeholder'=>'ID']) ?><br> <?=$this->Form->password("password",['class'=>'ppp','name'=>'password','placeholder'=>'パスワード']) ?> <br><?=$this->Form->submit("ログイン",['class'=>'ppp','value'=>'ログイン']) ?> <?=$this->Form->end(); ?> <br> <hr width="30%" size="1" color="#f6f6f6"> <br> <?php } ?> </div>
上記を前提に、誤りのご指摘のほどよろしくお願いします。
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。