PHP code example of casbin / swoole-redis-watcher

1. Go to this page and download the library: Download casbin/swoole-redis-watcher library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

casbin / swoole-redis-watcher example snippets




use Casbin\Enforcer;
use CasbinWatcher\SwooleRedis\Watcher;

Co::set(['hook_flags'=> SWOOLE_HOOK_ALL]); 

$http = new Swoole\Http\Server('0.0.0.0', 9501);

$http->on('WorkerStart', function ($server, $worker_id) {
    global $enforcer;

    // Initialize the Watcher.
    $watcher = new Watcher([
        'host' => '127.0.0.1',
        'password' => '',
        'port' => 6379,
        'database' => 0,
    ]);

    // Initialize the Enforcer.
    $enforcer = new Enforcer("path/to/model.conf", "path/to/policy.csv");

    // Set the watcher for the enforcer.
    $enforcer->setWatcher($watcher);

    // By default, the watcher's callback is automatically set to the
    // $enforcer->loadPolicy() in the setWatcher() call.
    // We can change it by explicitly setting a callback.
    $watcher->setUpdateCallback(function () use ($enforcer) {
        // Now should reload all policies.
        // $enforcer->loadPolicy();
    });

};

$http->on('Request', function ($request, $response) {
    // ...
});

$http->start();