質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

Q&A

解決済

1回答

6238閲覧

CakePHPのAuth認証について

shiritai

総合スコア11

CakePHP

CakePHPは、PHPで書かれたWebアプリケーション開発用のフレームワークです。 Ruby on Railsの考え方を多く取り入れており、Railsの高速性とPHPの機動性を兼ね備えています。 MVCやORMなどを「規約優先の考え方」で利用するため、コードを書く手間を省くことができます。 外部のライブラリに依存しないので、単体での利用が可能です。

0グッド

0クリップ

投稿2017/02/17 11:14

編集2017/02/17 11:30

###質問内容
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>

上記を前提に、誤りのご指摘のほどよろしくお願いします。

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

UsersControllerのinitialize()内でparent::initialize();を実行していないため、AppControllerのAuthコンポーネントのロードが実行されていないので、UsersControllerで$this->Authがfalseになっているためだと思います。

エラーメッセージはfalse(boolean)はidentify()というメソッドを持っていないので、エラーという意味です。

投稿2017/02/17 13:30

編集2017/02/17 13:34
popobot

総合スコア6586

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問