2013-08-07 41 views
0

我正在嘗試關注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'); 
    } 
} 
+2

發佈您的控制器代碼可能很有幫助,登錄功能應該放在login.ctp中。鑑於在你的用戶控制器,你有登錄功能與示例 – Sixthpoint

+0

代碼我猜用戶正在登錄,並被重定向到'UsersController index.ctp'成功登錄後(請發佈'AppController'代碼確認)。問題在於您沒有創建索引的視圖,也沒有登錄功能無法正常工作的任何視圖。登錄表單應當位於'View/Users/login.ctp'確定 – bowlerae

+0

我只是重新閱讀您的問題。所以你沒有任何問題,實際登錄到網站,只是你的'add()'搞砸了?你有'add.ctp'視圖文件嗎? – bowlerae

回答

1

從評論的談話扯...

「我仍然認爲問題是你錯過了index.ctp視圖文件。如果你ar e試圖添加一個用戶,然後這將解釋爲什麼當您的登錄表單位於View/Users/login.ctp時因爲沒有位於View/Users/index.ctp的文件而導致缺少視圖的錯誤消息,這是add()操作在添加用戶後將您重定向到的位置成功「。

+1

是的。我隱藏了本教程中的這條評論:**以同樣的方式,我們爲博客文章創建了視圖,或者使用代碼生成工具,我們實現了視圖。爲了本教程的目的,我們將只顯示add.ctp **。我添加/app/View/Users/index.ctp只有一個「添加用戶」鏈接。 – jacknad