PHP code example of dacheng-php / yii2-swoole

1. Go to this page and download the library: Download dacheng-php/yii2-swoole 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/ */

    

dacheng-php / yii2-swoole example snippets




return [
    'bootstrap' => [
        [
            'class' => \Dacheng\Yii2\Swoole\Bootstrap::class,
            'componentId' => 'swooleHttpServer',
            'hookFlags' => SWOOLE_HOOK_ALL,
        ],
    ],
    'components' => [
        'request' => [
            'class' => \Dacheng\Yii2\Swoole\Runtime\CoroutineRequest::class,
            'cookieValidationKey' => 'change-me',
        ],
        'swooleHttpServer' => [
            'class' => \Dacheng\Yii2\Swoole\Server\HttpServer::class,
            'host' => '127.0.0.1',
            'port' => 9501,
            'documentRoot' => __DIR__ . '/../web',
            'dispatcher' => new \Dacheng\Yii2\Swoole\Server\RequestDispatcher(__DIR__ . '/web.php'),
            'settings' => [
                'max_coroutine' => 100000,
            ],
        ],
        'db' => [
            'class' => \Dacheng\Yii2\Swoole\Db\CoroutineDbConnection::class,
            'dsn' => 'mysql:host=127.0.0.1;dbname=myapp',
            'username' => 'root',
            'password' => '',
            'charset' => 'utf8mb4',
            'poolMaxActive' => 20,
            'poolWaitTimeout' => 5.0,
        ],
        'redis' => [
            'class' => \Dacheng\Yii2\Swoole\Redis\CoroutineRedisConnection::class,
            'hostname' => '127.0.0.1',
            'port' => 6379,
            'poolMaxActive' => 20,
            'poolWaitTimeout' => 5.0,
        ],
        'cache' => [
            'class' => \Dacheng\Yii2\Swoole\Cache\CoroutineRedisCache::class,
            'redis' => 'redis',
        ],
        'queue' => [
            'class' => \Dacheng\Yii2\Swoole\Queue\CoroutineRedisQueue::class,
            'redis' => 'redis',
            'channel' => 'queue',
            'concurrency' => 10,
        ],
    ],
];

'controllerMap' => [
    'swoole' => [
        'class' => \Dacheng\Yii2\Swoole\Console\SwooleController::class,
        'serverComponent' => 'swooleHttpServer',
    ],
],

$rows = Yii::$app->db->createCommand('SELECT * FROM user WHERE status = :status', [
    ':status' => 1,
])->queryAll();

Yii::$app->redis->set('demo:key', 'value');

Yii::$app->queue->push(new SendEmailJob([
    'email' => '[email protected]',
]));
bash
composer 
bash
php yii swoole/start
bash
php yii swoole/stop