3

メンバー ビューにログイン フォーム (login.ctp) があります。しかし、URL を /login として指定すると、メンバー ビューのログインを表示する代わりに、ユーザー ビューのログイン ページが表示されます。

/Views/login.ctp ファイル

<div class="members form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Member'); ?>
<fieldset>
    <legend><?php echo __('Please enter your username and password'); ?></legend>
    <?php echo $this->Form->input('username');
    echo $this->Form->input('password');
?>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div> 

アプリ コントローラー ファイル

class AppController extends Controller {
//...

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
    )
);

public function beforeFilter() {
    $this->Auth->allow('index', 'view');
}
//...
}

私の routes.php ファイル

 Router::connect('/', array('controller' => 'members', 'action' => 'index'));
Router::connect('/login', array('controller' => 'members', 'action' => 'login'));

すべての Cookie を消去しました。間違いはどこですか?

もう1つ、ユーザーコントローラーを削除しようとしましたが、ユーザーコントローラーを削除すると、次のエラーが発生します...

エラー: UsersController が見つかりませんでした。

これは、MembersController.php からのログイン関数です。

         public function login() {
    if ($this->request->is('post')) {

        if ($this->Auth->login()) {
            $this->redirect($this->Auth->redirect());
        } else {
            $this->Session->setFlash(__('Invalid username or password, try again'));
        }
    }
}

//メンバー モデルのコード。

class Member extends AppModel {

/**
 * Validation rules
 *
 * @var array
 */
public $validate = array(
    'firstname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'lastname' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'email' => array(
        'email' => array(
            'rule' => array('email'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'password' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'age' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'address' => array(
        'notempty' => array(
            'rule' => array('notempty'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'phone' => array(
        'numeric' => array(
            'rule' => array('numeric'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
    'created_on' => array(
        'datetime' => array(
            'rule' => array('datetime'),
            //'message' => 'Your custom message here',
            //'allowEmpty' => false,
            //'required' => false,
            //'last' => false, // Stop validation after this rule
            //'on' => 'create', // Limit validation to 'create' or 'update' operations
        ),
    ),
);

function beforeSave() {
  if(isset($this->data[$this->alias]['password']))
    $this->data[$this->alias]['password'] = Security::hash($this->data[$this->alias]['password'], null, true);
  return true;
}
}
4

2 に答える 2

1

コンポーネント配列を次のように変更してみてください。

public $components = array(
    'Session',
    'Auth' => array(
        'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
        'logoutRedirect' => array('controller' => 'members', 'action' => 'index'),
        'Form' => array(
            'userModel' => 'Member'
        ),
    )
);

または、beforeFilter()でこれを試してください:

// Pass settings in using 'all'
$this->Auth->authenticate = array(
    AuthComponent::ALL => array('userModel' => 'Member'),
    'Form',
    'Basic'
);
于 2013-01-25T06:21:52.780 に答える
1

編集

テスト済みのコードとプロセス

AppController コード

<?php
class AppController extends Controller
{
    public $components = array
    (
        'Session',
        'Auth' => array
        (
            'loginRedirect' => array('controller' => 'members', 'action' => 'home'),
            'logoutRedirect' => array('controller' => 'members', 'action' => 'index')
        )
    );

    public function beforeFilter()
    {
        $this->Auth->allow('index', 'view');
        $this->Auth->fields = array('username' => 'email', 'password' => 'password');
        $this->Auth->userModel = 'Member';
    }
}

あなたのコードはすべて正常に動作しており、必要なのは に設定Auth->userModelするだけMemberです。

メンバーコントローラー

<?php
class MembersController extends AppController
{

    var $name = 'Members';

    function beforeFilter()
    {
        parent::beforeFilter();
    }

}
?>

そして、メンバーコントローラーで定義しますbeforeFilter as above.

于 2013-01-25T06:14:29.993 に答える