swoole-foundation/yii2-swoole-extension

在 Swoole 服务器上运行 yii2 Web 应用程序

1.0.2 2019-05-20 09:22 UTC

This package is auto-updated.

Last update: 2024-08-30 01:07:27 UTC


README

在 Swoole 环境中运行 Yii2 应用程序。

此扩展基于组件驱动开发。

不会对您的业务或 Yii2 框架产生任何副作用。

中文文档

入门

  1. 初始化您的 Yii 应用程序

    composer create-project --prefer-dist yiisoft/yii2-app-basic basic
  2. 使用 composer 安装此包

    composer require swoole-foundation/yii2-swoole-extension
  3. 创建服务器配置文件。

    // config/server.php
    <?php
      return [
       'host' => 'localhost',
       'port' => 9501,
       'mode' => SWOOLE_PROCESS,
       'sockType' => SWOOLE_SOCK_TCP,
       'app' => require __DIR__ . '/swoole.php', 
       'options' => [ // options for swoole server
           'pid_file' => __DIR__ . '/../runtime/swoole.pid',
           'worker_num' => 2,
           'daemonize' => 0,
           'task_worker_num' => 2,
       ]
    ];
  4. 创建 swoole.php 并替换 Yii2 的默认 Web 组件。

    感谢 @RicardoSette

    // config/swoole.php
    <?php
    
    $config = require __DIR__ . '/web.php';
    
    $config['components']['response']['class'] = swoole\foundation\web\Response::class;
    $config['components']['request']['class'] = swoole\foundation\web\Request::class;
    $config['components']['errorHandler']['class'] = swoole\foundation\web\ErrorHandler::class;
    
    return $config;
  5. 创建引导文件。

// bootstrap.php
<?php
/**
 * @author xialeistudio
 * @date 2019-05-17
 */

use swoole\foundation\web\Server;
use Swoole\Runtime;

// Warning: singleton in coroutine environment is untested!
Runtime::enableCoroutine();
defined('YII_DEBUG') or define('YII_DEBUG', true);
defined('YII_ENV') or define('YII_ENV', getenv('PHP_ENV') === 'development' ? 'dev' : 'prod');

require __DIR__ . '/vendor/autoload.php';
require __DIR__ . '/vendor/yiisoft/yii2/Yii.php';

// require your server configuration
$config = require __DIR__ . '/config/server.php';
// construct a server instance
$server = new Server($config);
// start the swoole server
$server->start();
  1. 启动您的应用程序。
php bootstrap.php
  1. 恭喜!您的第一个 Yii2 Swoole 应用程序正在运行!

示例

tests 目录中有一个完整的应用程序。

待办事项

  • 修复协程环境
  • 支持 Docker
  • 添加测试用例
  • 与 travis-ci 合作

贡献

由于像您这样的用户的贡献,此项目才能得以实现!

  1. 分支此项目
  2. 创建您的分支
  3. 提交 pull request
  4. 等待合并