1

How do I get a simple proof-of-concept REST API working in CakePHP 4?

I have followed the guides in CakePHP's cookbook here and here

Currently my routes.php file contains the following (with comments removed):

<?php

use Cake\Routing\Route\DashedRoute;
use Cake\Routing\RouteBuilder;

return static function (RouteBuilder $routes) {        
    $routes->setRouteClass(DashedRoute::class);    
    $routes->scope('/', function (RouteBuilder $builder) {            
        $builder->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);            
        $builder->connect('/pages/*', 'Pages::display');            
        $builder->fallbacks();
    });
    $routes->scope('/', function (RouteBuilder $builder) {
        $builder->setExtensions(['json']);
        $builder->resources('LocateITAPI');
    });
};

My Controller contains the following code:

<?php
// src/Controller/LocateITAPIController.php
namespace App\Controller;

use App\Controller\AppController;

class LocateITAPIController extends AppController
{
    public function initialize(): void
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
    }

    public function index()
    {        
        echo json_encode(utf8_encode("LocateITAPIController::index()"));
    }

    public function view($id)
    {        
        echo json_encode(utf8_encode("LocateITAPIController::view()"));
    }

    public function add()
    {        
        echo json_encode(utf8_encode("LocateITAPIController::add()"));
    }

    public function edit($id)
    {        
        echo json_encode(utf8_encode("LocateITAPIController::edit()"));
    }

    public function delete($id)
    {        
        echo json_encode(utf8_encode("LocateITAPIController::delete()"));
    }

}

I am trying to access it using the browser at:

http://localhost/app/locateitapi.json

I am expecting to see a JSON response, instead what i am seeing is this error:

Missing Controller Error

1 Answer 1

2

Due to the cakephp naming convention, you have that error message.

Solution I:

use url like: http://localhost/app/locate-i-t-a-p-i.json

Solution II: Add in router:

//http://localhost/app/locateitapi.json
$builder->connect('/locateitapi', ['controller' => 'LocateITAPIController', 'action' => 'index']);
$builder->connect('/locateitapi/add', ['controller' => 'LocateITAPIController', 'action' => 'add']);

Solution III:

rename your controller class like:

class LocateItApiController ...
// http://localhost/app/locate-it-api.json

class LocateitapiController ...
// http://localhost/app/locateitapi.json
Sign up to request clarification or add additional context in comments.

4 Comments

Thank you very much for the response. In general, would you advise staying away from that camel-case notation for CakePHP that i employed for Controllers?
Thank you @Salines - i employed solution 3, as it's the most readable and stays closest to the documentation of creating a proof-of-concept REST API app.
I might need to open a new question for this, but I thought I'd take a chance and ask here. After implementing solution 3, I now get an error 500: { "message": "Template file Locateitapi\\json\\index.php could not be found., ... "url": "\/locateitapi.json", "code": 500, "file": "Locateitapi\\json\\index.php", "line": 1359 }
I was able to resolve this by creating the responses in those template files and moving them to a json folder in inside template->Locateitapi->json

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.