我正在嘗試關注Simple Authentication and Authorization Application教程 ,但不清楚登錄函數(下面)的模板視圖文件應該放在哪裏。登錄功能的模板視圖文件應該去哪裏?
<div class="users form">
<?php echo $this->Session->flash('auth'); ?>
<?php echo $this->Form->create('User'); ?>
<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>
放在應用程序時/瀏覽/用戶/ login.ctp,用戶/添加導致以下錯誤:
Error: The view for UsersController::index() was not found.
Error: Confirm you have created the file: app\View\Users\index.ctp
當應用程序/瀏覽/用戶/ login.ctp更名爲應用程序/查看/用戶/ index.ctp,用戶/加表示
The user has been saved
但要回博客教程根和試圖做一個附加導致
Error: The view for UsersController::login() was not found.
Error: Confirm you have created the file: C:\csvn\www\jack\app\View\Users\login.ctp
所以:login函數的模板視圖文件應該放在哪裏?在索引,登錄或其他一些.ctp?
編輯:添加應用程序/控制器/ UsersController.php代碼
class UsersController extends AppController {
public function beforeFilter() {
parent::beforeFilter();
$this->Auth->allow('add'); // Letting users register themselves
}
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'));
}
}
}
public function logout() {
$this->redirect($this->Auth->logout());
}
public function index() {
$this->User->recursive = 0;
$this->set('users', $this->paginate());
}
public function view($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
$this->set('user', $this->User->read(null, $id));
}
public function add() {
if ($this->request->is('post')) {
$this->User->create();
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
}
}
public function edit($id = null) {
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->request->is('post') || $this->request->is('put')) {
if ($this->User->save($this->request->data)) {
$this->Session->setFlash(__('The user has been saved'));
$this->redirect(array('action' => 'index'));
} else {
$this->Session->setFlash(__('The user could not be saved. Please, try again.'));
}
} else {
$this->request->data = $this->User->read(null, $id);
unset($this->request->data['User']['password']);
}
}
public function delete($id = null) {
if (!$this->request->is('post')) {
throw new MethodNotAllowedException();
}
$this->User->id = $id;
if (!$this->User->exists()) {
throw new NotFoundException(__('Invalid user'));
}
if ($this->User->delete()) {
$this->Session->setFlash(__('User deleted'));
$this->redirect(array('action' => 'index'));
}
$this->Session->setFlash(__('User was not deleted'));
$this->redirect(array('action' => 'index'));
}
}
編輯:添加的AppController
<?php
App::uses('Controller', 'Controller');
class AppController extends Controller {
public $helpers = array('Session');
public $components = array(
'Session',
'Auth' => array(
'loginRedirect' => array('controller' => 'posts', 'action' => 'index'),
'logoutRedirect' => array('controller' => 'pages', 'action' => 'display', 'home'),
'authorize' => array('Controller') // Added this line
)
);
public function isAuthorized($user) {
// Admin can access every action
if (isset($user['role']) && $user['role'] === 'admin') {
return true;
}
// Default deny
return false;
}
public function beforeFilter() {
$this->Auth->allow('index', 'view');
}
}
發佈您的控制器代碼可能很有幫助,登錄功能應該放在login.ctp中。鑑於在你的用戶控制器,你有登錄功能與示例 – Sixthpoint
代碼我猜用戶正在登錄,並被重定向到'UsersController index.ctp'成功登錄後(請發佈'AppController'代碼確認)。問題在於您沒有創建索引的視圖,也沒有登錄功能無法正常工作的任何視圖。登錄表單應當位於'View/Users/login.ctp'確定 – bowlerae
我只是重新閱讀您的問題。所以你沒有任何問題,實際登錄到網站,只是你的'add()'搞砸了?你有'add.ctp'視圖文件嗎? – bowlerae