我正在開發我的第一個CakePHP項目。我以前只在程序PHP中工作過,而且我沒有處理過任何類似Cake框架的問題,所以我遇到了一些問題。我非常感謝幫助我的登錄腳本,這是行不通的。爲什麼我的CakePHP登錄腳本不工作?
我的應用程序將是橄欖球俱樂部的系統。因此,登錄頁面要求用戶從下拉列表中選擇他們的俱樂部,然後輸入密碼。
編輯:
工作如下:
/App/Webroot/View/Clubs/login.ctp
<div class="clubs form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('Club'); ?>
<fieldset>
<legend><?php echo __('Please select your club and enter your password'); ?></legend>
<table>
<tr>
<?php
echo $this->Form->input('id', array('label' => 'Club name', 'before' => '<td>', 'after' => '</td>', 'between' => '</td><td>', 'type'=>'select','options'=>$clubs));
?>
<tr>
<?php echo $this->Form->input('password', array('before' => '<td>', 'after' => '</td>', 'between' => '</td><td>'));
?>
</tr>
</table>
</fieldset>
<?php echo $this->Form->end(__('Login')); ?>
</div>
/App/Controller/AppController.php
public $components = array(
'Session',
'Auth' => array(
'authenticate' => array(
'Form' => array(
'userModel' => 'Club',
'fields' => array(
'username' => 'id',
'password' => 'password'
)
)
),
'loginRedirect' => array('controller' => 'clubs', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'loginAction' => array('admin' => false, 'controller' => 'clubs', 'action' => 'login')
),
'DebugKit.Toolbar'
);
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('logout', 'display');
}
/App/Controller/ClubsController.php
public function login() {
if ($this->request->is('post')) {
if ($this->Auth->login()) {
return $this->redirect($this->Auth->redirectUrl());
}
else {
$this->Session->setFlash(__('Invalid club or password, try again.', 'default', array(), 'auth'));
$this->set('clubs', $this->Club->find(
'list', array(
'fields' => array('club_name'),
'conditions' => array('status' => '2')
)
)
);
}
}
else {
$this->set('clubs', $this->Club->find(
'list', array(
'fields' => array('club_name'),
'conditions' => array('status' => '2')
)
)
);
}
}
public function logout() {
$this->Session->setFlash(__('You have been logged out.'));
$this->redirect($this->Auth->logout());
}
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add', 'login', 'index', 'logout', 'view');
}
我設法解決它。我不完全確定如何。我會很快發佈經過編輯的工作代碼。 – gazareth 2013-03-04 00:30:25