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

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

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

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

Q&A

解決済

1回答

1012閲覧

cakePHP3 $this->Auth->allow()のエラー

matsuma

総合スコア0

CakePHP

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

0グッド

0クリップ

投稿2021/06/16 08:03

cakePHP3を使っています
authコンポーネントを使ってログイン機能を作っている途中です。
しかし、ログインするとCall to a member function allow() on boolean のエラーが出てしまいます。
会員登録やログアウト等もした途端に同様のエラーが出ました。
AppControllerのloadComponent中にAuthもあり、どうすればエラーが解消されるのか分からない状態です。
エラーで指された行はAppController.phpの最後 $this->Auth->allow(['login', 'add', 'logout', 'index']);でした。
初心者で簡単な事であれば申し訳ないのですが解決策をご教示いただきたいです。

PHP

1//AppController.php 2<?php 3/** 4 * CakePHP(tm) : Rapid Development Framework (https://cakephp.org) 5 * Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 6 * 7 * Licensed under The MIT License 8 * For full copyright and license information, please see the LICENSE.txt 9 * Redistributions of files must retain the above copyright notice. 10 * 11 * @copyright Copyright (c) Cake Software Foundation, Inc. (https://cakefoundation.org) 12 * @link https://cakephp.org CakePHP(tm) Project 13 * @since 0.2.9 14 * @license https://opensource.org/licenses/mit-license.php MIT License 15 */ 16namespace App\Controller; 17 18use Cake\Controller\Controller; 19use Cake\Event\Event; 20 21/** 22 * Application Controller 23 * 24 * Add your application-wide methods in the class below, your controllers 25 * will inherit them. 26 * 27 * @link https://book.cakephp.org/3.0/en/controllers.html#the-app-controller 28 */ 29class AppController extends Controller 30{ 31 32 /** 33 * Initialization hook method. 34 * Use this method to add common initialization code like loading components. 35 * e.g. `$this->loadComponent('Security');` 36 * @return void 37 */ 38 public function initialize(){ 39 parent::initialize(); 40 $this->loadComponent('RequestHandler', [ 41 'enableBeforeRedirect' => false, 42 ]); 43 $this->loadComponent('Flash'); 44 $this->loadComponent('Auth', [ 45 'authorize' => ['Controller'], 46 //ログイン処理をどのプログラムで行うか 47 'loginAction' => [ 48 'controller' => 'Users', 49 'action' => 'login' 50 ], 51 //ログイン後どのページに移動するか 52 'loginRedirect' => [ 53 'controller' => 'MomResults', 54 'action' => 'index' 55 ], 56 //ログアウト後どのページに移動するか 57 'logoutRedirect' => [ 58 'controller' => 'MomResults', 59 'action' => 'index' 60 ], 61 'authenticate' =>[ 62 'Form' => [ 63 'fields' => [ 64 'username' => 'userid', 65 'password' => 'password' 66 ] 67 ] 68 ] 69 ]); 70 } 71 72 public function isAuthorized($user){ 73 // 管理者はすべての操作にアクセスできる 74 if (isset($user['role']) && $user['role'] === 'admin') { 75 return true; 76 } 77 // その他は拒否 78 return false; 79 } 80 81 public function beforeFilter(Event $event){ 82 //ログインなしでもアクセスできるページ 83 $this->Auth->allow(['login', 'add', 'logout', 'index']); 84 } 85}

PHP

1//UsersController.php 2<?php 3namespace App\Controller; 4 5use App\Controller\AppController; 6use Cake\Event\Event; 7/** 8 * Users Controller 9 * @property \App\Model\Table\UsersTable $Users 10 * @method \App\Model\Entity\User[]|\Cake\Datasource\ResultSetInterface paginate($object = null, array $settings = []) 11 */ 12 13class UsersController extends AppController 14{ 15/** 16 * @param Event $event 17 * @return \Cake\Http\Response|null|void 18 * ログイン認証機能について 19 */ 20 public function beforeFilter(Event $event) { 21 parent::beforeFilter($event); 22 //ユーザーの登録とログアウトを許可 23 $this->Auth->allow(['add', 'logout']); 24 } 25/** 26 * Index method 27 * @return \Cake\Http\Response|null 28 */ 29 public function index() { 30 $this->set('users', $this->Users->find('all')); 31 } 32 /** 33 * login method 34 * @return \Cake\Http\Response|null 35 */ 36 public function login() { 37 if ($this->request->is('post')) { 38 $user = $this->Auth->identify(); 39 if ($user) { 40 $this->Auth->setUser($user); 41 return $this->redirect($this->Auth->redirectUrl()); 42 } else { 43 $this->Flash->error(_('ユーザーIDまたはパスワードが間違っています')); 44 } 45 } 46 } 47 /** 48 * login method 49 */ 50 public function logout() { 51 return $this->redirect($this->Auth->logout()); 52 $this->Flash->success('ログアウトしました。'); 53 } 54 55 /** 56 * isAuthorized method 57 * 権限の詳細 58 */ 59 public function isAuthorized($user) { 60 $id = $this->request->getParam('pass.0'); 61 62 if ($id == $user['id']) { 63 return true; 64 } 65 66 return false; 67 } 68 /** 69 * View method 70 * @param string|null $id User id. 71 * @return \Cake\Http\Response|null 72 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 73 */ 74 public function view($id = null) { 75 $user = $this->Users->get($id); 76 $this->set(compact('user')); 77 } 78 79 /** 80 * Add method 81 * @return \Cake\Http\Response|null Redirects on successful add, renders view otherwise. 82 * ユーザー登録 83 */ 84 public function add() { 85 $user = $this->Users->newEntity(); 86 if ($this->request->is('post')) { 87 $user = $this->Users->patchEntity($user, $this->request->getData()); 88 if ($this->Users->save($user)) { 89 $this->Flash->success(__('会員登録完了しました!')); 90 return $this->redirect(['controller' => 'MomResults', 'action' => 'index']); 91 } 92 $this->Flash->error(__('会員登録できませんでした')); 93 } 94 $this->set(compact('user')); 95 } 96 97 98 /** 99 * Edit method 100 * 101 * @param string|null $id User id. 102 * @return \Cake\Http\Response|null Redirects on successful edit, renders view otherwise. 103 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 104 */ 105 public function edit($id = null) { 106 $user = $this->Users->get($id, [ 107 'contain' => [] 108 ]); 109 if ($this->request->is(['patch', 'post', 'put'])) { 110 $user = $this->Users->patchEntity($user, $this->request->getData()); 111 if ($this->Users->save($user)) { 112 $this->Flash->success(__('更新できました.')); 113 return $this->redirect(['controller' => 'Users', 'action' => 'view']); 114 } 115 $this->Flash->error(__('更新できませんでした')); 116 } 117 $this->set(compact('user')); 118 } 119 120 /** 121 * Delete method 122 * 123 * @param string|null $id User id. 124 * @return \Cake\Http\Response|null Redirects to index. 125 * @throws \Cake\Datasource\Exception\RecordNotFoundException When record not found. 126 */ 127 public function delete($id = null) { 128 $this->request->allowMethod(['post', 'delete']); 129 $user = $this->Users->get($id); 130 if ($this->Users->delete($user)) { 131 $this->Flash->success(__('会員削除できました')); 132 } else{ 133 $this->Flash->error(__('会員情報削除できませんでした')); 134 } 135 return $this->redirect(['controller' => 'Users', 'action' => 'index']); 136 } 137}

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

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

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

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

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

退会済みユーザー

退会済みユーザー

2021/06/16 12:37

特に問題ないように見えますが、、、`AppController::initialize` の `loadComponent('Auth')` の直後に `$this->Auth` の内容をログ出力してみてはいかがでしょうか?(問題なく出力されるようなら `AppController::beforeFilter` の `$this->Auth->allow()` の直前でも)
matsuma

2021/06/17 07:24

ご回答ありがとうございます。 ログの出力、確認をしたことがなかったのでためになりました! AppControllerのinitializeを他のコントローラーで呼び出してないのが原因だったみたいです。 ありがとうございました!
guest

回答1

0

自己解決

AppControllerのinitializeを他のコントローラーで呼び出してないのが原因だったみたいです。
他のコントローラー全ての一番上に
public function initialize(){parent::initialize();}
を記入することによって解決できました。

投稿2021/06/18 00:34

matsuma

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.48%

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

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

質問する

関連した質問