1

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]

4
  • What happens when you go to "http://localhost/cake/tasks Commented Mar 2, 2016 at 14:22
  • It shows the list of tasks in a web view. Commented Mar 2, 2016 at 14:22
  • What router extensions have you defined? Commented Mar 2, 2016 at 14:58
  • I have not defined any router extensions. It should automate the json extension right? Commented Mar 2, 2016 at 14:58

1 Answer 1

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

I though that it would be set as default to allow json response.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.