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

wmdvanzyl
  • 352
  • 2
  • 5
  • 17

1 Answers1

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
Salines
  • 5,674
  • 3
  • 25
  • 50
  • 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? – wmdvanzyl Mar 04 '22 at 12:15
  • 1
    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. – wmdvanzyl Mar 04 '22 at 12:38
  • 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 } – wmdvanzyl Mar 04 '22 at 13:02
  • 1
    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 – wmdvanzyl Mar 04 '22 at 13:08