登录功能的模板视图文件应该放在哪里?

时间:2013-08-07 16:21:49

标签: cakephp authentication

我正在尝试关注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>

当放入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表示

  

用户已保存

但是回到博客教程root并尝试进行添加会导致

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');
    }
}

1 个答案:

答案 0 :(得分:1)

从评论对话中拉出来......

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