2014-10-09 70 views
0

我正在編寫一個Web服務來驗證用戶登錄是否有效。下面是放置在UsersController.php中的Web服務的簡單實現:Web服務登錄以檢查在cakephp中驗證用戶

public function webservice_login() 
{ 
    $this->autoRender = false; 
    if ($this->request->is('post')) 
    { 
     if ($this->Auth->login()) 
     { 
      echo json_encode(array('ok_msg' => 'User authentication success')); 
     } 
     else 
     { 
      echo json_encode(array('fail_msg' => 'User authentication failure')); 
     }    
    } 
} 

它不起作用。我收到的錯誤信息是這樣的;

\n\tError: \n\tPostsController could not be found. 

\n 
\n\tError: \n\tCreate the class PostsController below in file: app\\Controller\\PostsController.php 

\n 
\n<?php\nclass PostsController extends AppController {\n\n}\n 
\n 
\n\tNotice: \n\tIf you want to customize this error message, create app\\View\\Errors\\missing_controller.ctp 

代碼有什麼問題?我應該如何重寫Web服務?我正在使用Cakephp 2.5。奇怪的是,我沒有一個名爲Post的控制器。

+0

你可以發送請求的網址,你收到錯誤? – engvrdr 2014-10-09 11:10:22

+0

你沒有控制器的帖子?那麼你的控制者是什麼?您是否使用過$ this-> Auth-> allow()作爲webservice_login存在的函數 – Abhishek 2014-10-09 11:52:14

+0

顯示您要調用的URL。 – burzum 2014-10-09 12:32:26

回答

1

的錯誤是非常明確,剛讀錯誤消息:

\n\tError: \n\tPostsController could not be found.

確保您的控制器存在。

另外你實現JSON響應的方式並不是很CakePHP的。請參閱http://book.cakephp.org/2.0/en/views/json-and-xml-views.html如何做得更好。

我建議您使用JSEND作爲響應格式來傳達狀態。

+0

有沒有問題,它實際上是代表標籤空間,我認爲這應該是其他原因 – Abhishek 2014-10-09 11:49:46

+0

阿布舍克是正確的。 \ t代表標籤空間。 – user781486 2014-10-09 12:41:28

0

確保您使用的不是短期的手PHP標籤:

此錯誤可能是由於使用短的PHP標記即"<? ?>"。我建議你使用"<?php ?>"