Home
Quick start
Documentation
Forum
GitHub
Home
Quick start
Documentation
Forum
GitHub
  • Quick Start

    • Introduction
    • Why Phico
    • Installation
  • Example app

    • Scaffolding
    • Routes
    • Requests
    • Responses
    • Events
  • Phico

    • CLI
    • Config
    • Container
    • Filesystem
    • Logger
    • Middleware
    • Request
    • Response
    • Router
    • Support
    • View
  • Libraries

    • Authentication
    • Cache
    • CDN
    • Database
    • Http client
    • Locale
    • Mailer
    • Profiler
    • Query
    • Queue
    • Validation
    • Session
    • View
  • Works

    • Introduction

Events

Phico has a simple events system built in.

Listeners

Listeners can be callables or invokable classes. In the example apps they are configured in boot/events.php.

<?php
// listen for the blog browse error event
$events->add('blog.browse.error', function ($event) {
    // get the event id
    $id = $event->id();
    // get the event context (a readonly Capsule instance)
    $context = $event->context();
    // do something with the event
    logger()->error(
        "got event: {$id}",
        $context->only('request')
    );
});

Event

The Event object is a simple container for the context which is a Capsule instance. It has two methods

  • id() Returns the event id string.
  • context() Returns the readonly context.

Notify listeners

Use the event() helper to notify listeners, it accepts two arguments:

  • id a string describing the event.
  • data an optional array of context.
<?php
// fire this event if the blog browse request fails
event('blog.browse.error', [
    'request' => $request,
]);
Last Updated:
Contributors: indgy
Prev
Responses