huangdijia / laravel-trigger
MySQL trigger base on MySQLReplication.
Fund package maintenance!
huangdijia
hdj.me/sponsors
Installs: 4 536
Dependents: 0
Suggesters: 0
Security: 0
Stars: 29
Watchers: 4
Forks: 1
Open Issues: 0
Requires
- php: ^8.2
- illuminate/console: ^11.0
- illuminate/support: ^11.0
- krowinski/php-mysql-replication: ^8.0
Requires (Dev)
- huangdijia/php-coding-standard: ^2.1
- orchestra/testbench: ^9.0
- phpstan/phpstan: ^1.11
- dev-main / 6.x-dev
- v6.0.0-beta.3
- v6.0.0-beta.2
- v6.0.0-beta.1
- 5.x-dev
- v5.0.0-beta.1
- 4.x-dev
- v4.1.0
- v4.0.3
- v4.0.2
- v4.0.1
- 4.0.0
- 3.x-dev
- v3.0.1
- 3.0.0
- 2.x-dev
- v2.2.1
- 2.2.0
- 2.1.4
- 2.1.3
- 2.1.2
- 2.1.1
- 2.1.0
- 2.0.3
- 2.0.2
- 2.0.1
- 2.0.0
- 1.2.1
- 1.2.0
- 1.1.15
- 1.1.14
- 1.1.13
- 1.1.12
- 1.1.11
- 1.1.10
- 1.1.9
- 1.1.8
- 1.1.7
- 1.1.6
- 1.1.5
- 1.1.4
- 1.1.3
- 1.1.2
- 1.1.1
- 1.1.0
This package is auto-updated.
Last update: 2024-12-07 01:29:16 UTC
README
Subscribe MySQL events like jQuery, base on php-mysql-replication
MySQL server settings
In your MySQL server configuration file you need to enable replication:
[mysqld]
server-id = 1
log_bin = /var/log/mysql/mysql-bin.log
expire_logs_days = 10
max_binlog_size = 100M
binlog_row_image = full
binlog-format = row #Very important if you want to receive write, update and delete row events
Mysql replication events explained https://dev.mysql.com/doc/internals/en/event-meanings.html
Mysql user privileges:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'user'@'host'; GRANT SELECT ON `dbName`.* TO 'user'@'host';
Installation
Laravel
install
composer require "huangdijia/laravel-trigger:^4.0"
publish config
php artisan vendor:publish --provider="Huangdijia\Trigger\TriggerServiceProvider"
Lumen
install
composer require "huangdijia/laravel-trigger:^4.0"
edit bootstrap/app.php
add:
$app->register(Huangdijia\Trigger\TriggerServiceProvider::class); ... $app->configure('trigger');
publish config and route
php artisan trigger:install [--force]
Configure
edit .env
, add:
TRIGGER_HOST=192.168.xxx.xxx TRIGGER_PORT=3306 TRIGGER_USER=username TRIGGER_PASSWORD=password ...
Usage
php artisan trigger:start [-R=xxx]
Subscriber
<?php namespace App\Listeners; use Huangdijia\Trigger\EventSubscriber; class ExampeSubscriber extends EventSubscriber { public function onUpdate(UpdateRowsDTO $event) { // } public function onDelete(DeleteRowsDTO $event) { // } public function onWrite(WriteRowsDTO $event) { // } }
more subscriber usage
Event Route
common
$trigger->on('database.table', 'write', function($event) { /* do something */ });
multi-tables and multi-evnets
$trigger->on('database.table1,database.table2', 'write,update', function($event) { /* do something */ });
multi-events
$trigger->on('database.table1,database.table2', [ 'write' => function($event) { /* do something */ }, 'update' => function($event) { /* do something */ }, ]);
action as controller
$trigger->on('database.table', 'write', 'App\\Http\\Controllers\\ExampleController'); // call default method 'handle' $trigger->on('database.table', 'write', 'App\\Http\\Controllers\\ExampleController@write');
action as callable
class Foo { public static function bar($event) { dump($event); } } $trigger->on('database.table', 'write', 'Foo@bar'); // call default method 'handle' $trigger->on('database.table', 'write', ['Foo', 'bar']);
action as job
Job
namespace App\Jobs; class ExampleJob extends Job { private $event; public function __construct($event) { $this->event = $event; } public function handle() { dump($this->event); } }
Route
$trigger->on('database.table', 'write', 'App\Jobs\ExampleJob'); // call default method 'dispatch' $trigger->on('database.table', 'write', 'App\Jobs\ExampleJob@dispatch_now');
Event List
php artisan trigger:list [-R=xxx]
Terminate
php artisan trigger:terminate [-R=xxx]