2016-03-02 57 views
1

我准备了一个控制器,如下所示。用cakephp控制器返回json响应时出错

class TasksController extends AppController 
{  
    public function index() 
    { 
     $this->paginate = [ 
      'contain' => ['Users'], 
      'conditions' => [ 
       'Tasks.user_id' => $this->Auth->user('id'), 
      ] 
     ]; 
     $tasks = $this->paginate($this->Tasks); 
     $this->set(compact('tasks')); 

     //$this->set('tasks', $this->paginate($this->Tasks)); 
     $this->set('_serialize', ['tasks']); 
    } 
} 

和我的AppController如下。

class AppController extends Controller 
{ 
    public function initialize() 
    { 
     parent::initialize(); 

     $this->loadComponent('RequestHandler'); 
     $this->loadComponent('Flash'); 
     $this->loadComponent("Auth", [ 
      'authorize' => 'Controller', 
      'authenticate' => [ 
       'Form' => [ 
        'fields' => [ 
         'username' => 'email', 
         'password' => 'password' 
        ] 
       ] 
      ], 
      'loginAction' => [ 
       'controller' => 'Users', 
       'action' => 'login' 
      ], 
      'unauthorizedRedirect' => $this->referer() 
     ]); 

     $this->Auth->allow(['display']); 
    } 
} 

当我输入以下URL“http://localhost/cake/tasks.json”时,出现以下错误。

Error: Tasks.jsonController could not be found. 

Error: Create the class Tasks.jsonController below in file: src/Controller/Tasks.jsonController.php 


<?php 
namespace App\Controller; 

use App\Controller\AppController; 

class Tasks.jsonController extends AppController 
{ 

} 

什么是这里的问题,以及如何解决呢无需添加路由或JSON视图文件。 [我正在使用CakePHP 3.0]

+0

当你进入''http:// localhost/cake/tasks'时会发生什么 – jszobody

+0

它显示了web视图中的任务列表 –

+0

你定义了哪些路由器扩展 – drmonkeyninja

回答

2

它看起来像你没有让蛋糕知道预计.json文件扩展名。结果蛋糕正在寻找Tasks.jsonController。你想在你的config/routes.php文件文件补充一点: -

Router::extensions(['json']); 

参见routing file extensions的文档了解更多详情。

+0

虽然它会被设置为默认允许json响应。 –