# Cakephp 5 Global Event Manager not execute

**URL:** <https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817>\
**Category:** Uncategorized\
**Created:** [January 21, 2024, 4:10pm UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817 "2024-01-21T16:10:40Z")\
**Posts on this page:** 16\
**Page:** 1

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 21, 2024, 4:10pm UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/1 "2024-01-21T16:10:40Z")

</div>

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.

 ![Screenshot from 2024-01-21 21-29-21](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/04e0f8556cdc636ebe5e7c266009e05c2965f878.png)

here is afterSave method so it works perfectly fine:-

 ![Screenshot from 2024-01-21 21-32-17](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/a/aeb60c6ea2af05d6a877c2faf61a3640e8c3b84f.png)

I set here in global event:- NotificationEvent

 ![Screenshot from 2024-01-21 21-39-22](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/7/7de6f1f52b41ba06cc221cbbcda6d139bbc930b4.png)

please give me any idea how it will be work.? docs not help for this now

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 21, 2024, 5:22pm UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/2 "2024-01-21T17:22:19Z")

</div>

`src/EventListeners/TestContext.php`

```auto
<?php
declare(strict_types=1);

namespace App\EventListeners;

use Cake\Event\Event;
use Cake\Event\EventListenerInterface;

class TestContext implements EventListenerInterface
{
    /**
     * @return array<string, mixed>
     */
    public function implementedEvents(): array
    {
        return [
            'Notification.Published' => 'published',
        ];
    }

    /**
     * @param \Cake\Event\Event $event The event
     * @return void
     */
    public function published(Event $event): void
    {
        $data = $event->getData('entity');
    }
}

```

in controller

```auto
public function edit(?int $id = null)
{
    $article = $this->Articles->get($id);

    $this->dispatchEvent('Notification.Published', [
        'entity' => $article
    ]);
}

```

in config/bootstrap.php

```auto
$eventManger = \Cake\Event\EventManager::instance();
$eventManger->on(new \App\EventListeners\TestContext());

```

works fine for me.

* * *

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.

* * *

Also be aware, that the 2nd param of the [Event constructor is the subject](https://github.com/cakephp/cakephp/blob/5.x/src/Event/Event.php#L84), not the data present in the event.

* * *

Usually you shouldn’t care about manually creating event instances since thats what the [EventDispatcherTrait](https://api.cakephp.org/5.0/trait-Cake.Event.EventDispatcherTrait.html) is for which both [Controllers](https://github.com/cakephp/cakephp/blob/5.x/src/Controller/Controller.php#L102) as well as [Models](https://github.com/cakephp/cakephp/blob/5.x/src/ORM/Table.php#L164) use by default.

Thats why my example just does `$this->dispatchEvent()`

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 22, 2024, 5:01am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/3 "2024-01-22T05:01:20Z")

</div>

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!!

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 22, 2024, 5:37am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/4 "2024-01-22T05:37:16Z")

</div>

Any Idea @KevinPfeifer why not composer install this package

 ![Screenshot from 2024-01-22 11-04-42](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/c/c61aef76acde1ce2417ff5eb3c6c3948647a841f.png)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 22, 2024, 7:40am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/5 "2024-01-22T07:40:24Z")

</div>

You need to install the 3.0-RC Version of that plugin.

```auto
composer require dereuromark/cakephp-ajax:^3.0-RC

```

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 22, 2024, 7:54am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/6 "2024-01-22T07:54:14Z")

</div>

yo!!! man thank’s @KevinPfeifer it works  
thanks a lot

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 25, 2024, 5:57am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/7 "2024-01-25T05:57:00Z")

</div>

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

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 25, 2024, 6:59am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/8 "2024-01-25T06:59:21Z")

</div>

[https://book.cakephp.org/4/en/controllers/components/request-handling.html](https://book.cakephp.org/4/en/controllers/components/request-handling.html)  
[https://book.cakephp.org/4/en/appendices/4-4-migration-guide.html#requesthandlercomponent](https://book.cakephp.org/4/en/appendices/4-4-migration-guide.html#requesthandlercomponent)

[![](https://img.youtube.com/vi/GZPUlQEnAkA/maxresdefault.jpg "CakePHP 4 Tutorial 2023 - Simple JSON API (Part 18)") ](https://www.youtube.com/watch?v=GZPUlQEnAkA)

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 25, 2024, 11:09am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/9 "2024-01-25T11:09:15Z")

</div>

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

cakephp 4x working fine

 ![Screenshot from 2024-01-25 16-37-22](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/7/7b0872f1d80d065330361fc9e431e29f91118b3e.png)

But in cakephp 5x does not throw in json format error

 ![Screenshot from 2024-01-25 16-37-30](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/0/0edc0916b27a2f46ac440db1c133f2885891a1e0.png)

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 25, 2024, 11:24am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/10 "2024-01-25T11:24:48Z")

</div>

You need to adjust the exceptionRenderer for that. See [Cakephp default error response but i need to json format error response - #8 by KevinPfeifer](https://discourse.cakephp.org/t/cakephp-default-error-response-but-i-need-to-json-format-error-response/10829/8)

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 25, 2024, 11:28am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/11 "2024-01-25T11:28:05Z")

</div>

nope!! its not working in cakephp 5x version @KevinPfeifer… Mixture Api

---

<div class="post-metadata">

**Author:** ![KevinPfeifer](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/kevinpfeifer/32/3365_2.png) [@KevinPfeifer](https://discourse.cakephp.org/u/KevinPfeifer)\
**Post date:** [January 25, 2024, 11:36am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/12 "2024-01-25T11:36:29Z")

</div>

I never said you need to use mixerapi, I just said you need to set a custom exceptionRenderer.

But let me hand feed the info to you: There is no ready made plugin yet for CakePHP 5 so in your case you have to copy the plugin [GitHub - mixerapi/exception-render: Handles rendering entity validation errors and other exceptions for your API [READ-ONLY]](https://github.com/mixerapi/exception-render) to your own app and adjust it to CakePHP 5 (if there are changes necessary at all)

Just because the plugin can’t be installed via composer doesn’t mean its not compatible with CakePHP 5.

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [January 25, 2024, 3:52pm UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/13 "2024-01-25T15:52:24Z")

</div>

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`):

```php
$this->addViewClasses([\Cake\View\JsonView::class]);

```

**[https://book.cakephp.org/5/en/controllers.html#content-type-negotiation](https://book.cakephp.org/5/en/controllers.html#content-type-negotiation)**

If you need more control over the behavior, then you’d possibly need a customized exception renderer as noted by @KevinPfeifer

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 27, 2024, 8:23am UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/14 "2024-01-27T08:23:54Z")

</div>

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

 ![Screenshot from 2024-01-27 13-52-12](https://canada1.discourse-cdn.com/flex029/uploads/cakephp/original/2X/c/cc0e69c2bb4cafeb948635950a98101fbc8a2286.png)

---

<div class="post-metadata">

**Author:** ![ndm](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/ndm/32/2234_2.png) [@ndm](https://discourse.cakephp.org/u/ndm)\
**Post date:** [January 27, 2024, 4:04pm UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/15 "2024-01-27T16:04:15Z")

</div>

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:

```php
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.

---

<div class="post-metadata">

**Author:** ![shaan007](https://yyz1.discourse-cdn.com/flex029/user_avatar/discourse.cakephp.org/shaan007/32/2765_2.png) [@shaan007](https://discourse.cakephp.org/u/shaan007)\
**Post date:** [January 27, 2024, 4:22pm UTC](https://discourse.cakephp.org/t/cakephp-5-global-event-manager-not-execute/11817/16 "2024-01-27T16:22:23Z")

</div>

hahahhaah… thanks @ndm its real working & helping me thanks man… now my application get ready for my client.
