0

我正在尝试遵循简单的身份验证和授权应用程序教程,但不清楚登录功能的模板视图文件(如下)应该去哪里。

<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>

当放入 app/View/Users/login.ctp 时, users/add 会导致以下错误:

Error: The view for UsersController::index() was not found.
Error: Confirm you have created the file: app\View\Users\index.ctp

当 app/View/Users/login.ctp 重命名为 app/View/Users/index.ctp 时, users/add 表示

用户已保存

但是回到博客教程根目录并尝试添加原因

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

那么:登录功能的模板视图文件应该放在哪里呢?在索引、登录或其他一些 .ctp 中?

编辑:添加 app/Controller/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');
    }
}
4

1 回答 1

1

从评论对话中提取...

“我仍然认为问题在于您缺少index.ctp视图文件。如果您尝试添加用户,那么这将解释为什么您在登录表单位于时收到缺少视图的错误消息,View/Users/login.ctp因为您没有找到文件成功添加用户后,操作会将您重定向到的位置。View/Users/index.ctpadd()

于 2013-08-07T19:28:35.103 回答