Incorrect Routing
spacebiscuit opened this issue · comments
I have the CRUD plugin configured and working. However when I make a POST request to:
/controller.json
It calls the index action and not the Add action. I have confirmed in the headers of the request that this is a POST request.
My controller is as follows:
use \Crud\Controller\ControllerTrait;
public function initialize() {
parent::initialize();
$this->loadComponent(
'Crud.Crud', [
'actions' => [
'Crud.Add',
],
'listeners' => ['Crud.Api'],
]
'RequestHandler'
);
$this->Crud->config(['listeners.api.exceptionRenderer' => 'App\Error\ExceptionRenderer']);
$this->Crud->addListener('relatedModels', 'Crud.RelatedModels');
}
I have routing setup with:
$routes->extensions(['json', 'xml', 'ajax']);
What am I doing wrong?
Which controller action is called depends on routes and Crud doesn't have anything to do with routing. Crud comes into the picture only after Cake has decided which action to use.
Closing as your issue is related to routing and not Crud
Ok I see I was missing:
$routes->resources('Purchases');
Thanks for the hint.
You might want to set up something like this if you are exposing multiple/lots of resources:
const API_RESOURCES = [
'Countries',
'Currencies'
];
Router::scope('/', function ($routes) {
foreach (API_RESOURCES as $apiResource) {
$routes->resources($apiResource, [
'inflect' => 'dasherize'
]);
}
});
When we have a Controller with two words in the name, the solution of having for example:
$routes->resources('FirmantesExternos');
Do not solve the problem.
I also have in the inflector inside bootstrap.php
Inflector::rules('irregular', [ ... 'firmanteexterno' => 'firmantesexternos', ... ]);
The POST requests keep resulting in Index method called.