下記のサイトを参考に、Cakephpで認証コンポーネントを使ったログイン処理を作成中なのですが、転送設定がループになっているようで、ログインページにアクセスしようとすると、エラーになってしまいます。
どのファイルでループが起きてしまっているか分からないので、教えていただきたいです。
ソース
AppController.php
php
1<?php 2 3namespace App\Controller; 4 5use Cake\Controller\Controller; 6use Cake\Event\Event; 7 8 9class AppController extends Controller 10{ 11 12 13 public function initialize() 14 { 15 parent::initialize(); 16 17 $this->loadComponent('RequestHandler', [ 18 'enableBeforeRedirect' => false, 19 ]); 20 $this->loadComponent('Flash'); 21 22 $this->loadComponent('Auth', [ 23 'loginAction' => [ 24 'controller' => 'Admins', 25 'action' => 'login' 26 ], 27 'loginRedirect' => [ 28 'controller' => 'Admins', 29 'action' => 'index' 30 ], 31 'logoutRedirect' => [ 32 'controller' => 'Admins', 33 'action' => 'login', 34 'home' 35 ], 36 'authenticate' => [ 37 'Form' => [ 38 'fields' => ['username' => 'username', 'password' => 'password'] 39 ] 40 ], 41 ]); 42 } 43 44 public function beforeFilter(Event $event) 45 { 46 $this->Auth->setConfig('authenticate', [ 47 'Form' => ['userModel' => 'admins'], 48 ]); 49 } 50} 51
AdminsController
php
1<?php 2namespace App\Controller; 3 4use App\Controller\AppController; 5use Cake\Event\Event; // ← 追加 6 7 8 9class AdminsController extends AppController 10{ 11 12 public function index() 13 { 14 15 $admins = $this->paginate($this->Admins); 16 17 $this->set(compact('admins')); 18 19 } 20 21 22 public function view($id = null) 23 { 24 $admin = $this->Admins->get($id, [ 25 'contain' => [] 26 ]); 27 28 $this->set('admin', $admin); 29 } 30 31 32 public function add() 33 { 34 $admin = $this->Admins->newEntity(); 35 if ($this->request->is('post')) { 36 $admin = $this->Admins->patchEntity($admin, $this->request->getData()); 37 if ($this->Admins->save($admin)) { 38 $this->Flash->success(__('The admin has been saved.')); 39 40 return $this->redirect(['action' => 'index']); 41 } 42 $this->Flash->error(__('The admin could not be saved. Please, try again.')); 43 } 44 $this->set(compact('admin')); 45 } 46 47 48 public function edit($id = null) 49 { 50 $admin = $this->Admins->get($id, [ 51 'contain' => [] 52 ]); 53 if ($this->request->is(['patch', 'post', 'put'])) { 54 $admin = $this->Admins->patchEntity($admin, $this->request->getData()); 55 if ($this->Admins->save($admin)) { 56 $this->Flash->success(__('The admin has been saved.')); 57 58 return $this->redirect(['action' => 'index']); 59 } 60 $this->Flash->error(__('The admin could not be saved. Please, try again.')); 61 } 62 $this->set(compact('admin')); 63 } 64 65 66 public function delete($id = null) 67 { 68 $this->request->allowMethod(['post', 'delete']); 69 $admin = $this->Admins->get($id); 70 if ($this->Admins->delete($admin)) { 71 $this->Flash->success(__('The admin has been deleted.')); 72 } else { 73 $this->Flash->error(__('The admin could not be deleted. Please, try again.')); 74 } 75 76 return $this->redirect(['action' => 'index']); 77 } 78 79 80 public function beforeFilter(Event $event) 81 { 82 parent::beforeFilter($event); 83 $this->Auth->allow(['add', 'hoge']); 84 } 85 86 public function login() 87 { 88 if ($this->request->is('post')) { 89 $admin = $this->Auth->identify(); 90 if ($admin) { 91 $this->Auth->setUser($admin); 92 return $this->redirect($this->Auth->redirectUrl()); 93 } 94 $this->Flash->error(__('ユーザ名もしくはパスワードが間違っています')); 95 } 96 } 97 98 public function logout() 99 { 100 return $this->redirect($this->Auth->logout()); 101 } 102} 103
URL
よろしくお願いします。
あなたの回答
tips
プレビュー