我正在做我的第一个 CakePHP 项目。我以前只在程序 PHP 中工作过,而且我没有处理过像 Cake 框架这样的东西,所以遇到了一些初期问题。我非常感谢我的登录脚本的帮助,该脚本不起作用。
我的应用程序将成为橄榄球俱乐部的系统。因此,登录页面要求用户从下拉列表中选择他们的俱乐部,然后输入密码。
编辑:
工作如下:
/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');
}