2013-02-26 100 views
1

由于我在我的cakephp项目中实施了授权,因此它无法使用JSON访问我的视图。我是否必须在任何地方设置特定设置才能再次进行此项工作? 我希望加入自动完成检查allowedActions和isAuthorized会做的伎俩在CakePHP中执行授权后,JSON视图不起作用

应用程序\控制器\ BrandsController.php(从不必要的代码剥离)

<?php 
    App::uses('Sanitize', 'Utility'); 

    class BrandsController extends AppController { 
     public $helpers = array('Html', 'Form', 'Session'); 
     public $components = array('Session', 'RequestHandler'); 

     public function beforeFilter() { 
      parent::beforeFilter(); 
      $this->Auth->allowedActions = array('autoComplete'); 
     } 

     public function isAuthorized($user) { 
      if ($this->action === 'view' || $this->action === 'index' || $this->action === 'autoComplete') { 
       return true; 
      } 
      return parent::isAuthorized($user); 
     } 

     public function autoComplete($name = null) 
     { 
      if(!$name) 
      { 
       $name = @$this->params->query['name']; 
      } 

      $name = Sanitize::paranoid($name, array(' ')); 

      if (!$name || empty($name)) 
      { 
       new NotFoundException(__('Invalid searchquery')); 
      } 
      else 
      { 

       $objects = $this->Brand->find('list', 
        array(
         'conditions' => array("Brand.name LIKE" => "%" . $name . "%") 
        ) 
       ); 
       $this->layout = 'ajax'; 
       $this->RequestHandler->setContent('json', 'application/json'); 
       $this->set(compact('objects')); 
       $this->render('json/output_json'); 
      } 
     } 
    } 
?> 

应用程序\查看\品牌\ JSON \ output_json.ctp

<?php 
    Configure::write('debug', 0); 
    echo json_encode($objects); 
?> 

呼叫

<?php 
    $brandsAutoCompleteUrl = Router::url(array('controller' => 'brands', 'action' => 'autoComplete')); 
?> 
<script type="text/javascript"> 
    $(document).ready(function(){ 
     $.getJSON('<?php echo $brandsAutoCompleteUrl; ?>', { name: 'test' }, function(data) { 
      // never called 
     }); 
    }); 
</script> 

在我的Chrome调试窗口,它最高审计机关

GET http://localhost/cakephp/brands/autoComplete?name=tes 500 (Internal Server Error) 

当我直接调用这个网址从我的浏览器,我得到预期的结果

+1

随着error500消息,您应检查的第一件事是你webserv的日志文件呃,它可能与auth组件没有关系。 – thaJeztah 2013-02-26 22:19:35

+1

此外,与您的问题没有直接关系,但您需要更新CakePHP 2.3的代码。 '$ this-> params'仅用于向后兼容性,您应该使用请求对象; '$ this-> request-> query('name')'(请不要用'@'来压制错误)。 requestHandler也不是必需的,而是使用'response'对象。更多信息在这里; http://book.cakephp.org/2.0/en/controllers/request-response.html#dealing-with-content-types,这里http://book.cakephp.org/2.0/zh/views/json-and -xml-意见。html – thaJeztah 2013-02-26 22:27:36

+1

代码中的一些错误:您不会'抛出'您的异常,只是创建它(这可能会导致错误),并且您没有禁用autoRender,这可能会导致错误 – thaJeztah 2013-02-26 22:30:35

回答

2

我通过更换

固定这个问题
$this->RequestHandler->setContent('json', 'application/json'); 

通过

$this->response->type(array('json' => 'application/json')); 
1

正确的允许是

$this->Auth->allow('view', 'index'); 

这是可以做到只在控制器是允许的操作的所有者 - Link to documentation

此外 - 在firefox萤火虫或在铬中,您可以查看响应你的500 HTTP错误,通常是一个确切的CakePHP错误的HTML代码 - 首先进行调查。

为什么在这个控制器中添加组件和助手而不是AppController?这些组件对于任何控制器都很常见 - 将它们放到AppController是合理的。
如果您还没有注意到 - AppController的所有帮助器和组件都与辅助器和子控制器的组件合并在一起。您无法通过更换子控制器中的组件来关闭Auth组件。

建议:
避免使用'@'代码,如@$this->params->query['name'];。在“如果”条件

如果你不知道使用!empty($this->params->query['name'])empty($name) == !$name,也!$name可以触发变量$名称错误,不会启动 - http://www.php.net/manual/ru/types.comparisons.php(1台)

+0

感谢所有的指针,我在日志文件中找到了这个bug。不能相信我在这里问自己之前没有自己检查过 – Mark 2013-02-27 18:40:19