2014-10-09 46 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 ?>"