REST setup in routes.php is not working when having two words controller names

48 views Asked by At

We are using CakePHP 3.10.*, friendsofcake/crud ^5.0 and friendsofcake/crud-json-api ^0.4.0. Lately we've been having trouble getting requests to the FirmantesExternosController to be handled according to REST conventions. Where, when sending a POST request, cakephp is expected to execute the "add" action of the controller. But instead, it executes the "index" action.

Here is our router scope inside routes.php

Router::scope('/', function (RouteBuilder $routes) {

$routes->setExtensions(['json','xml']);
...(other resources)...
$routes->resources('Firmantes');
$routes->resources('FirmantesExternos');

$routes->connect('/', ['controller' => 'Pages', 'action' => 'display', 'home']);
$routes->connect('/pages/*', ['controller' => 'Pages', 'action' => 'display']);

$routes->fallbacks(DashedRoute::class);

});

We also have in bootstrap.php

Inflector::rules('irregular', [
  ...(other inflections)...
  'firmante' => 'firmantes',
  'firmanteexterno' => 'firmantesexternos',
]);

And inside AppController.php

class AppController extends Controller {

use \Crud\Controller\ControllerTrait;

public function initialize()
{
    parent::initialize();

    $this->loadComponent('RequestHandler');

    $this->loadComponent('Crud.Crud', [
      'actions' => [
        'index' => [
            'className' => 'Crud.Index',
            'relatedModels' => true
        ],
        'view' => [
            'className' => 'Crud.View',
            'relatedModels' => true
        ],
        'add' => [
            'className' => 'Crud.Add',
            'relatedModels' => false
        ],
        'edit' => [
            'className' => 'Crud.Edit',
            'relatedModels' => false
        ],
        'delete' => [
            'className' => 'Crud.Delete',
            'relatedModels' => false
        ]
      ],
      'listeners' => [
          'Crud.Api',
          'Crud.RelatedModels',
          'CrudJsonApi.JsonApi'
      ]
    ]);
}
}

The FirmantesExternosController.php

<?php
namespace App\Controller;

use App\Controller\AppController;

class FirmantesExternosController extends AppController
{

}

The FirmantesController.php

<?php
namespace App\Controller;

use App\Controller\AppController;

class FirmantesController extends AppController
{

}

We are using crud component application wide as seen in AppController. Having the FirmantesController working as intended with the REST conventions after puting $routes->resources('firmantes'); in routes.php. But we do the same with FirmantesExternosController and it doesn't work. What are we missing?

1

There are 1 answers

1
erwin.s.h On

THANKS A LOT! To all comments and suggestions.

I solved the problem doing the following:

  1. Make sure to use "case sensitive controller names" in the $routes->resources('...'); lines. In this case $routes->resources('FirmantesExternos'); is the correct format.

  2. In the console inside de cakephp project, run bin/cake routes to figure the route format that cakephp uses. And then make sure to use these in your requests to the api.