PHP code example of casbin / workerman-redis-watcher

1. Go to this page and download the library: Download casbin/workerman-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 / workerman-redis-watcher example snippets




use Casbin\Enforcer;
use CasbinWatcher\WorkermanRedis\Watcher;
use Workerman\Worker;


$worker = new Worker();
$worker->count = 2;
$worker->onWorkerStart = function () {

    // 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");

    $enforcer->setWatcher($watcher);
    
    // Set callback, sets the callback function that the watcher will call,
    // When the policy in DB has been changed by other instances.
    // A classic callback is $enforcer->loadPolicy().
    $watcher->setUpdateCallback(function () use ($enforcer) {
        echo "Now should reload all policies." . PHP_EOL;
        $enforcer->loadPolicy();
    });
};

Worker::runAll();