here is edit method so i want to save data into notification table it would save through event system, but its is not working when i am edit the record in contrast add method (new record) insert then Event fired.
You gotta know, that Controllers and Models each have their own EventManager instance. But all events get âbubbled upâ to the global event manager as well so you should always be able to register event listeners on the global event manager.
Usually you shouldnât care about manually creating event instances since thats what the EventDispatcherTrait is for which both Controllers as well as Models use by default.
Thats why my example just does $this->dispatchEvent()
Hi!! @KevinPfeifer thankâs brother for replying and help me here⌠my code also well fine, I just find another issue which was existed in NotificationEvent.php file so I just fix there and now⌠events are working fine⌠great!!
hi @KevinPfeifer how are you? @KevinPfeifer do you know that RequestHandler->renderAs how it work in cakephp 5. Actually cakephp 4x $this->RequestHandler->renderAs($this,âjsonâ) I was using this mehod for display json format data from Api side with .json extension and error also print in json format but now in cakephp 5x RequestHandler->renderAs not working because cakephp 5x removed it⌠is there any method which perform same functionality as in cakephp 4x
yes i know all that @KevinPfeifer but this view Json class does not work if any Error triggered, I mean Error triggered message show in web view but i want to show all errors in json view⌠please look at that screenshots
In CakePHP 4.x JSON error responses are handled transparently by the request handler component, which doesnât exist in 5.x anymore.
If all you want is to bring back that behavior, and youâre using negotiable requests (ie they have a Accepts header), then that behavior can be enabled by adding the respective view class to the error controller.
So if you wanted to add JSON support, youâd add the following call in ErrorController::initialize() (located in src/Controller/ErrorController.php):
thank @ndm its really help me and i got error message in json response, can tell me one thing more here if i do not set negotiable requests⌠without json extension then how can i print json error message. Now i am getting web based error message
Actually want to throw json error response error when Any User/Client open /api/v1 url without any specific endpoint
There are many different ways in which that could be achieved. One would be instead of using negotiation, to directly set the respective view class in the controllers that should be affected, depending on the requested endpoint if necessary.
So in your error controllerâs initialize() method you could do something like this:
if ($this->request->getParam('controller') === 'Api') {
$this
->viewBuilder()
->setClassName(\Cake\View\JsonView::class);
}
Depending on the exact route that youâre trying to match you may have to add further conditions for the action, prefix, and/or plugin params.