cakephpを最近勉強し始めました。
簡単な会員制掲示板を勉強していますが、
登録された情報でログインできません。
(メールアドレスとパスワードが一致していればログインできるはずですが、
「メールアドレスかパスワードが間違っています」と表示されます)
どこに原因があるのかが未だにわかっていないので
よろしければ教えて下さい。
■補足
PostController
php
1<?php 2 3class PostsController extends AppController { 4 5 public $uses = array('User');//ユーザーテーブルの呼び出し 6 7 public $components = array( 8 'Session', 9 'Auth' => array( 10 'authenticate' => array( 11 'Form' => array( 12 'fields' => 13 array('email' => 'email', 'password' => 'password') //フォームの値 => fields 14 ) 15 ), 16 'loginRedirect' => array('action' => 'postlist'),//ログイン後のリダイレクト先 17 'logoutRedirect' => array('action' => 'index'),//ログアウト後のリダイレクト先 18 'loginAction' => array('action' => 'index'),//ログイン処理をどこで行うか 19 ) 20 ); 21 22 public function beforeFilter() { 23 $this->Auth->allow('index');//トップページはログインせずともアクセスできるようにする 24 } 25 26 public function index () 27 { 28 if ($this->request->is('post')) 29 { 30 if ($this->Auth->login()) 31 { 32 return $this->redirect($this->Auth->redirect()); 33 } 34 else 35 { 36 $this->Session->setFlash('メールアドレスかパスワードが間違っています', 'default', array(), 'auth'); 37 } 38 } 39 } 40 //var_dump($post); 41 42 public function postlist() { 43 44 } 45}
UsersController
php
1<?php 2 3 4class UsersController extends AppController { 5 6 public $components = array('Auth'); 7 8 public function beforeFilter() { 9 $this->Auth->allow('index', 'add');//addページにログインせずともアクセスできるようにする 10 } 11 12 public function index() { 13 14 } 15 16 public function add() { 17 $this->request->data['User']['password'] = 18 AuthComponent::password($this->request->data['User']['password']); 19 20 $this->User->save($this->request->data); 21 22 return $this->redirect( 23 array( 24 'controller' => 'posts', 25 'action' => 'index' 26 ) 27 ); 28 } 29}
Post/index.ctp(view)
php
1<h1>ログイン</h1> 2<div> 3 <?php echo $this->Session->Flash('auth'); ?> 4 <?php echo $this->Form->create('Post', array('action' => 'index')); ?> 5 <?php echo $this->Form->input('User.email', array('label' => 'メールアドレス')); ?> 6 <?php echo $this->Form->input('User.password', array('label' => 'パスワード')); ?> 7 <?php echo $this->Form->end('ログイン'); ?> 8</div> 9<?php echo $this->Html->link('新規登録はこちら!', array('controller' => 'users', 'action' => 'index')); ?>
Users/index.ctp(view)
php
1<h1>新規登録</h1> 2<?php echo $this->Form->create('User', array('action' => 'add')); ?> 3<?php echo $this->Form->input('User.name', array('label' => 'ユーザー名')); ?> 4<?php echo $this->Form->input('User.email', array('label' => 'メールアドレス')); ?> 5<?php echo $this->Form->input('User.password', array('label' => 'パスワード')); ?> 6<?php echo $this->Form->end('登録する'); ?> 7<?php echo $this->Html->link( 8 'ログイン画面へ', 9 array( 10 'controller' => 'posts', 11 'action' => 'index' 12 ) 13 ); 14 15?>
User.php(model)
php
1<?php 2 3class User extends AppModel { 4 5 public $validate = array( 6 'email' => array( 7 'rule' => 'notBlank' 8 ), 9 'password' => array( 10 'rule' => array('notBlank', 'password') 11 ) 12 ); 13}
postlist.ctp
php
1<h1>会員制掲示板へようこそ</h1>
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。