PHPackages                             deepziyu/swoft-yii2 - PHPackages - PHPackages  [Skip to content](#main-content)[PHPackages](/)[Directory](/)[Categories](/categories)[Trending](/trending)[Leaderboard](/leaderboard)[Changelog](/changelog)[Analyze](/analyze)[Collections](/collections)[Log in](/login)[Sign up](/register)

1. [Directory](/)
2. /
3. [Framework](/categories/framework)
4. /
5. deepziyu/swoft-yii2

ActiveLibrary[Framework](/categories/framework)

deepziyu/swoft-yii2
===================

85PHP

Since Jun 29Pushed 8y ago1 watchersCompare

[ Source](https://github.com/deepziyu/swoft-yii2)[ Packagist](https://packagist.org/packages/deepziyu/swoft-yii2)[ RSS](/packages/deepziyu-swoft-yii2/feed)WikiDiscussions master Synced 2w ago

READMEChangelogDependenciesVersions (1)Used By (0)

swoft-yii2
==========

[](#swoft-yii2)

完美滴在 swoft 框架中使用 yii2 的组件。

*此插件依赖于 [yii2-swoole](https://github.com/deepziyu/yii2-swoole) 实现*

已支持的组件
------

[](#已支持的组件)

- yii2-db 易框架的 MySQL-Connect 、 ActiveRecord 等
- yii2-log 易框架的日志组件，推荐还是用 swoft-log 吧
- yii2-cache 易框架的缓存组件，

安装
--

[](#安装)

#### 环境要求

[](#环境要求)

- swoft-v1.0 以上

#### composer install 插件安装

[](#composer-install-插件安装)

- 在项目中的 `composer.json` 文件中添加依赖：

```
{
  "require-dev": {
      "deepziyu/swoft-yii2": "dev-master"
  }
}
```

- 次插件依赖 Yii2-framework ，所以 composer 安装 Yii2 时会出现一些问题，具体解决方法参考 [Yii2-installation](https://www.yiiframework.com/doc/guide/2.0/zh-cn/start-installationinstallation)。
- 执行 `$ php composer.phar update` 或 `$ composer update` 进行安装。

配置
--

[](#配置)

在 swoft 项目的 config/app.php 中添加如下配置：

```
\Swoft\App::setAliases([
    // you can wirte it in `config/define.php` also
    '@swoft-yii2' => '@vendor/deepziyu/swoft-yii2/src'
]);
return [
    'bootScan'     => [
        // ···· other beans
        'deepziyu\swoft\yii' => \Swoft\App::getAlias('@swoft-yii2'),
    ],
    'beanScan'     => [
        // ···· other beans
        'deepziyu\swoft\yii' => \Swoft\App::getAlias('@swoft-yii2'),
    ],
    'yiiConfig' => require __DIR__ . DS . 'yii.php',
];
```

yii.php 文件配置就是常规的 Yii-Config 了:

```
return [
    'env' => 'dev', // YII_ENV 的值 VALUE
    'debug' => true,// YII_DEBUG 的值 VALUE
    'config' => [   // 常规的 Yii-Config
        'id' => 'swotf-test',
        'basePath' => BASE_PATH,
        'language' => 'zh-CN',
        'timeZone' => 'Asia/Shanghai',
        'bootstrap' => ['log'],
        'components' => [
            'request' => [
                'cookieValidationKey' => 'php is the best!!',
            ],
            'log' => [
                'traceLevel' => 3,
                'targets' => [
                    [
                        'class' => 'yii\log\FileTarget',
                        'levels' => ['error', 'warning'],
                    ],
                ],
            ],
            'db' => [
                'class' => 'deepziyu\yii\swoole\db\Connection',
                'dsn' => 'mysql:host=127.0.0.1;dbname=testDb',
                'username' => 'test',
                'password' => 'testP',
                'charset' => 'utf8',
                'enableReloadSchema' => true, // must be true
                'attributes' => [
                    //MysqlPoolPdo::POOL_MAX_SIZE => 50, 连接池的大小
                    //MysqlPoolPdo::POOL_MAX_SLEEP_Times => 0.1, 连接池满栈时等待的秒
                ],
                'slaves' => [
                    // 支持主从配置
                    // support Yii2 salves config
                ]
            ],
            'cache' => [
                'class' => 'yii\caching\ArrayCache',
            ],
        ]
    ],
];
```

愉快的使用咯
------

[](#愉快的使用咯)

```
    /**
     * 测试 AR 查询
     * @RequestMapping("/yii/ar")
     */
    public function yiiAR()
    {
        return [
            'query1' => \Yii::$app->getDb()->createCommand("select 1")->queryAll(),
            'query2' => (new Query())->from('tag')->where(['tagid' => 1])->one(),
            'query3' => (new class extends ActiveRecord
            {
                public static function tableName()
                {
                    return 'tag2';
                }

                public function rules()
                {
                    return [[['tagname', 'tagid'], 'safe']];
                }
            })->find()->asArray()->all(),

        ];
    }
```

more words 再多说几句
----------------

[](#more-words-再多说几句)

- 此插件会单独维护一份协程的上下文，和数据库连接池，所以会吃点性能咯
- 没有支持的组件最好不要用，嘿，容易出问题滴
- 我们不支持各种 defer 特性，这麒麟臂不好控制
- 此插件目前仅在 Swoole\\Http\\Server 模式下加载
- 在 Yii2-framework 会自己记录一下日志，比如 `Yii::error('hello')` 此类的，这些日志会记录在 SWOFT\_BASE\_PATH\\runtime\\logs\\app.log 里

Chat &amp;&amp; Help
--------------------

[](#chat--help)

Swoft 框架 QQ 群: 548173319 (@小紫羽)

###  Health Score

21

—

LowBetter than 18% of packages

Maintenance20

Infrequent updates — may be unmaintained

Popularity9

Limited adoption so far

Community4

Small or concentrated contributor base

Maturity41

Maturing project, gaining track record

How is this calculated?**Maintenance (25%)** — Last commit recency, latest release date, and issue-to-star ratio. Uses a 2-year decay window.

**Popularity (30%)** — Total and monthly downloads, GitHub stars, and forks. Logarithmic scaling prevents top-heavy scores.

**Community (15%)** — Contributors, dependents, forks, watchers, and maintainers. Measures real ecosystem engagement.

**Maturity (30%)** — Project age, version count, PHP version support, and release stability.

### Community

Maintainers

![](https://avatars.githubusercontent.com/u/13025775?v=4)[deepziyu](/maintainers/deepziyu)[@deepziyu](https://github.com/deepziyu)

### Embed Badge

![Health badge](/badges/deepziyu-swoft-yii2/health.svg)

```
[![Health](https://phpackages.com/badges/deepziyu-swoft-yii2/health.svg)](https://phpackages.com/packages/deepziyu-swoft-yii2)
```

###  Alternatives

[laravel/socialite

Laravel wrapper around OAuth 1 &amp; OAuth 2 libraries.

5.7k104.3M844](/packages/laravel-socialite)[laravel/dusk

Laravel Dusk provides simple end-to-end testing and browser automation.

1.9k38.6M289](/packages/laravel-dusk)[nineinchnick/edatatables

Grid widget for the Yii Framework, wrapper for the DataTables jQuery plugin

173.2k](/packages/nineinchnick-edatatables)[link-cloud/fast-hyperf

LinkCloud Fast Hyperf

241.2k1](/packages/link-cloud-fast-hyperf)

PHPackages © 2026

[Directory](/)[Categories](/categories)[Trending](/trending)[Changelog](/changelog)[Analyze](/analyze)
