1
votes

I have prepared a controller as below.

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']);
    }
}

and my AppController as below.

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']);
    }
}

When I enter the following URL, "http://localhost/cake/tasks.json" I get the following error.

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
{

}

What is the problem here and how do I fix it without adding route or json view file. [I am using the CakePHP 3.0]

1
What happens when you go to "http://localhost/cake/tasks - jszobody
It shows the list of tasks in a web view. - Ham Dong Kyun
What router extensions have you defined? - drmonkeyninja
I have not defined any router extensions. It should automate the json extension right? - Ham Dong Kyun

1 Answers

2
votes

It looks like you haven't let Cake know to expect the .json file extension. As a result Cake is looking for the Tasks.jsonController. You want to add this in your config/routes.php file:-

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

See the docs on routing file extensions for more details.