sergeymakinen/yii2-facades

该包已被弃用,不再维护。未建议替代包。

为 Yii 2 应用组件提供类似 Laravel 的门面支持

安装数: 1,542

依赖: 0

建议者: 0

安全: 0

星标: 20

关注者: 2

分支: 3

开放问题: 0

类型:yii2-extension

v1.5.3 2017-06-12 23:37 UTC

This package is not auto-updated.

Last update: 2020-05-01 18:46:21 UTC


README

Laravel 类似的门面支持为 Yii 2 应用组件。正是您想要的:简单、全面,并支持通过 PHPDoc 的 IDE 自动完成,因此您不会失望。

Code Quality Build Status Code Coverage SensioLabsInsight

Packagist Version Total Downloads Software License

目录

安装

安装此扩展的首选方法是通过 composer

运行

composer require "sergeymakinen/yii2-facades:^1.0"

或在您的 composer.json 文件的 require 部分中添加

"sergeymakinen/yii2-facades": "^1.0"

使用

基本上,您安装扩展并像使用所有正常的 Yii 2 应用组件一样使用它,但语法更简短、更简单。让我们看看(如果您好奇,所有默认门面,包括抽象基类 Facade,都位于 sergeymakinen\facades 命名空间下)

生成随机字符串

之前

$random = Yii::$app->security->generateRandomString(128);

之后

$random = Security::generateRandomString(128);

获取所有用户(仅示例!)

之前

$users = Yii::$app->db->createCommand('SELECT * FROM users;')->queryAll();

之后

$users = Db::createCommand('SELECT * FROM users;')->queryAll();

格式化货币

之前

$price = Yii::$app->formatter->asCurrency(123456.78, 'USD');

之后

$price = Formatter::asCurrency(123456.78, 'USD');

访问属性

任何类的公共属性 $foo 都可以通过访问器获取

$value = YourFacadeName::getFoo()

并设置

YourFacadeName::setFoo($value)

可用的门面

名称 门面组件别名 组件/接口
资产 Yii::$app->assetManager yii\web\AssetManager
认证 Yii::$app->auth yii\rbac\ManagerInterface
缓存 Yii::$app->cache yii\caching\Cache
数据库 Yii::$app->db yii\db\Connection
错误 Yii::$app->errorHandler yii\console\ErrorHandler
yii\web\ErrorHandler
格式化器 Yii::$app->formatter yii\i18n\Formatter
HTTP Yii::$app->httpClient yii\httpclient\Client
国际化 Yii::$app->i18n yii\i18n\I18N
日志 Yii::$app->log yii\log\Dispatcher
邮件发送器 Yii::$app->mailer yii\swiftmailer\Mailer
Redis Yii::$app->redis yii\redis\Connection
请求 Yii::$app->request yii\console\Request
yii\web\Request
响应 Yii::$app->response yii\console\Response
yii\web\Response
路由器 Yii::$app->urlManager yii\web\UrlManager
安全 Yii::$app->security yii\base\Security
会话 Yii::$app->session yii\web\Session
URL Yii::$app->urlManager yii\web\UrlManager
用户 Yii::$app->user yii\web\User
视图 Yii::$app->view yii\web\View

辅助工具

一些外观也包含有用的辅助工具,可以使得开发更加快速和优雅。

缓存

cache

public static function cache($key, $default, $duration = 0, $dependency = null)

使用提供的键获取值,如果值不在缓存中,将返回指定的默认值。如果值不在缓存中,它将被缓存。默认值也可以是一个闭包

$users = Cache::cache('users', function () {
    return app\models\Users::findAll();
}, 3600);

get

public static function get($key, $default = false)

使用提供的键获取值,并返回它或指定的默认值,该默认值也可以是一个闭包

$options = Cache::get('options', function () {
    return [
        'option1' => false,
        'option2' => true
    ];
});

响应

bare

public static function bare($statusCode = 204, array $headers = [])

返回一个包含可选头的空响应

public function actionCreate()
{
    // ...
    return Response::bare(201);
}

html

public static function html($data, array $headers = [])

返回一个包含可选头的HTML响应

public function actionIndex()
{
    // ...
    return Response::html($this->render('index'), [
        'Cache-Control' => 'no-cache'
    ]);
}

json

public static function json($data, array $headers = [])

返回一个包含可选头的JSON响应

public function actionList()
{
    // ...
    return Response::json(Db::createCommand('SELECT * FROM users')->all());
}

jsonp

public static function jsonp($data, $callback = 'callback', array $headers = [])

返回一个包含可选头的JSONP响应

public function actionApi($callback)
{
    // ...
    return Response::jsonp([
        'success' => true,
        'response' => $data
    ], $callback);
}

raw

public static function raw($data, array $headers = [])

返回一个包含数据“原样”和可选头的响应

public function actionCreate()
{
    // ...
    return Response::raw($binary, [
        'Content-Type' => 'application/octet-stream'
    ]);
}

xml

public static function xml($data, array $headers = [])

返回一个包含可选头的XML响应

public function actionCreate()
{
    // ...
    return Response::xml([
        'success' => true,
        'response' => $data
    ]);
}

扩展

如果您想创建一个新的外观,这很快也很容易,想象一下您想引入一个 YourFacadeName 外观

class YourFacadeName extends Facade
{
    /**
     * @inheritdoc
     */
    public static function getFacadeComponentId()
    {
        return 'yourFacadeComponentName'; // Yii::$app->yourFacadeComponentName
    }
}

然后每次您调用

YourFacadeName::hello('world');

它将按以下方式执行

Yii::$app->get('yourFacadeComponentName')->hello('world');