CakePHP
  • Documentation
    • Book
    • API
    • Videos
    • Reporting Security Issues
    • Privacy Policy
    • Logos & Trademarks
  • Business Solutions
  • Swag
  • Road Trip
  • Team
  • Community
    • Community
    • Get Involved
    • Issues (Github)
    • Bakery
    • Featured Resources
    • Training
    • Meetups
    • My CakePHP
    • CakeFest
    • Newsletter
    • Linkedin
    • YouTube
    • Facebook
    • Twitter
    • Mastodon
    • Help & Support
    • Forum
    • Stack Overflow
    • IRC
    • Slack
    • Paid Support
CakePHP

C CakePHP 6.0 API

  • Project:
    • CakePHP
      • CakePHP
      • Authentication
      • Authorization
      • Chronos
      • Elastic Search
      • Queue
  • Version:
    • 6.0
      • 6.0
      • 5.4
      • 5.3
      • 5.2
      • 5.1
      • 5.0
      • 4.6
      • 4.5
      • 4.4
      • 4.3
      • 4.2
      • 4.1
      • 4.0
      • 3.10
      • 3.9
      • 3.8
      • 3.7
      • 3.6
      • 3.5
      • 3.4
      • 3.3
      • 3.2
      • 3.1
      • 3.0
      • 2.10
      • 2.9
      • 2.8
      • 2.7
      • 2.6
      • 2.5
      • 2.4
      • 2.3
      • 2.2
      • 2.1
      • 2.0
      • 1.3
      • 1.2

Namespaces

  • Global
  • Cake
    • AttributeResolver
    • Cache
    • Collection
    • Command
    • Console
    • Container
    • Controller
    • Core
    • Database
    • Datasource
    • Error
    • Event
      • Attribute
      • Decorator
      • Exception
    • Form
    • Http
    • I18n
    • Lock
    • Log
    • Mailer
    • Network
    • ORM
    • Routing
    • TestSuite
    • Utility
    • Validation
    • View

Class EventListener

Declares an event listener for a class or method using PHP attributes.

When placed on a method, registers that method as the listener callable.

When placed on a class, resolves the callable using the following priority:

  1. The explicit $method argument when provided.
  2. __invoke() when present on the class.
  3. A method name inferred from the event name (e.g. Order.afterPlace → onOrderAfterPlace).

The attribute is repeatable, allowing a single method or class to listen to multiple events.

Examples

Method-level (most common):

#[EventListener('Order.afterPlace')]
#[EventListener('Order.afterCancel', priority: 20)]
public function updateMetrics(EventInterface $event): void {}

Class-level with explicit method:

#[EventListener('Order.afterPlace', method: 'handleOrder')]
class OrderListener {}

Class-level with invokable class:

#[EventListener('Order.afterPlace')]
class OrderListener {
    public function __invoke(EventInterface $event): void {}
}

Target a named event manager:

#[EventListener('Order.afterPlace', manager: 'orders')]
public function updateOrderMetrics(EventInterface $event): void {}
Namespace: Cake\Event\Attribute

Method Summary

  • __construct() public

    Initializes an event listener attribute definition.

Method Detail

__construct() ¶ public

__construct(string $event, int|null $priority = null, string|null $method = null, string|null $manager = null)

Initializes an event listener attribute definition.

Parameters
string $event

Event name to listen to (e.g. 'Order.afterPlace').

int|null $priority optional

Listener priority. When null, the receiving event manager's default priority is used at connection time.

string|null $method optional

Explicit method name to use as the listener callable. Only applicable for class-level attributes; ignored when the attribute is placed on a method.

string|null $manager optional

Named event manager to register the listener on. When null, the connector's primary event manager is used.

OpenHub
Pingping
Linode
  • Business Solutions
  • Showcase
  • Documentation
  • Book
  • API
  • Videos
  • Reporting Security Issues
  • Privacy Policy
  • Logos & Trademarks
  • Community
  • Get Involved
  • Issues (Github)
  • Bakery
  • Featured Resources
  • Training
  • Meetups
  • My CakePHP
  • CakeFest
  • Newsletter
  • Linkedin
  • YouTube
  • Facebook
  • Twitter
  • Mastodon
  • Help & Support
  • Forum
  • Stack Overflow
  • IRC
  • Slack
  • Paid Support

Generated using CakePHP API Docs