huangdijia/laravel-trigger
最新稳定版本:v6.0.0
Composer 安装命令:
composer require huangdijia/laravel-trigger
包简介
MySQL trigger base on MySQLReplication.
README 文档
README
Subscribe to MySQL events like jQuery, based on php-mysql-replication
Quick Start
- Install the package:
composer require "huangdijia/laravel-trigger:^4.0" - Configure your MySQL server for replication (see MySQL Server Configuration)
- Publish the config:
php artisan vendor:publish --provider="Huangdijia\Trigger\TriggerServiceProvider" - Configure your
.envfile with database credentials - Start listening:
php artisan trigger:start
Table of Contents
- Quick Start
- MySQL Server Configuration
- Installation
- Usage
- Event Subscribers
- Event Routes
- Management Commands
- Thanks to
MySQL Server Configuration
Replication 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
For more information: MySQL replication events explained
User Privileges
Grant the necessary privileges to your MySQL user:
GRANT REPLICATION SLAVE, REPLICATION CLIENT ON *.* TO 'user'@'host'; GRANT SELECT ON `dbName`.* TO 'user'@'host';
Installation
Laravel
Install via Composer:
composer require "huangdijia/laravel-trigger:^4.0"
Publish the configuration file:
php artisan vendor:publish --provider="Huangdijia\Trigger\TriggerServiceProvider"
Lumen
Install via Composer:
composer require "huangdijia/laravel-trigger:^4.0"
Edit bootstrap/app.php and add:
$app->register(Huangdijia\Trigger\TriggerServiceProvider::class); ... $app->configure('trigger');
Publish configuration and routes:
php artisan trigger:install [--force]
Configure
Edit your .env file and add the following configuration:
TRIGGER_HOST=192.168.xxx.xxx TRIGGER_PORT=3306 TRIGGER_USER=username TRIGGER_PASSWORD=password ...
Usage
Start the trigger service to begin listening for MySQL events:
php artisan trigger:start [-R=xxx]
The service will monitor your MySQL binary log and trigger registered event handlers when database changes occur.
Event Subscribers
Create a custom event subscriber by extending the EventSubscriber class:
<?php namespace App\Listeners; use Huangdijia\Trigger\EventSubscriber; use MySQLReplication\Event\DTO\UpdateRowsDTO; use MySQLReplication\Event\DTO\DeleteRowsDTO; use MySQLReplication\Event\DTO\WriteRowsDTO; class ExampleSubscriber extends EventSubscriber { public function onUpdate(UpdateRowsDTO $event) { // Handle UPDATE events } public function onDelete(DeleteRowsDTO $event) { // Handle DELETE events } public function onWrite(WriteRowsDTO $event) { // Handle INSERT events } }
For more subscriber usage examples, see: EventSubscribers
Event Routes
Basic Usage
$trigger->on('database.table', 'write', function($event) { /* do something */ });
Multi-tables and Multi-events
$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'); // calls 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'); $trigger->on('database.table', 'write', ['Foo', 'bar']);
Action as Job
Define your job class:
namespace App\Jobs; class ExampleJob extends Job { private $event; public function __construct($event) { $this->event = $event; } public function handle() { dump($this->event); } }
Register the job route:
$trigger->on('database.table', 'write', 'App\Jobs\ExampleJob'); // calls default method 'dispatch' $trigger->on('database.table', 'write', 'App\Jobs\ExampleJob@dispatch_now');
Management Commands
List Events
View all registered event listeners:
php artisan trigger:list [-R=xxx]
Terminate Service
Stop the trigger service gracefully:
php artisan trigger:terminate [-R=xxx]
Thanks to
统计信息
- 总下载量: 5.3k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 31
- 点击次数: 0
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2019-05-09