定制 jimchen/yii2-facades 二次开发

按需修改功能、优化性能、对接业务系统,提供一站式技术支持

邮箱:yvsm@zunyunkeji.com | QQ:316430983 | 微信:yvsm316

jimchen/yii2-facades

Composer 安装命令:

composer require jimchen/yii2-facades

包简介

Laravel like facades support for Yii 2 application components

README 文档

README

Laravel like facades support for Yii 2 application components. Just what you want: simple, extensive and with an IDE auto completion support via PHPDoc so you won't be disappointed.

Packagist Version Total Downloads Software License

Table of contents

Installation

The preferred way to install this extension is through composer.

Either run

composer require jimchen/yii2-facades

Usage

Basically you install the extension and start using it like you do with all normal Yii 2 application components but with a shorter simpler syntax, let's take a look (in case you wonder, all default facades, including an abstract base Facade reside under a jimchen\facades namespace):

Generate random string

Before:

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

After:

$random = Security::generateRandomString(128);

Fetch all users (just an example!)

Before:

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

After:

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

Format currency

Before:

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

After:

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

Access properties

Any class public property $foo can be got via an accessor:

$value = YourFacadeName::getFoo()

And set:

YourFacadeName::setFoo($value)

Available facades

Name Facaded component alias Component/interface
Asset Yii::$app->assetManager yii\web\AssetManager
Auth Yii::$app->auth yii\rbac\ManagerInterface
Cache Yii::$app->cache yii\caching\Cache
Db Yii::$app->db yii\db\Connection
Error Yii::$app->errorHandler yii\console\ErrorHandler
yii\web\ErrorHandler
Formatter Yii::$app->formatter yii\i18n\Formatter
I18n Yii::$app->i18n yii\i18n\I18N
Log Yii::$app->log yii\log\Dispatcher
Mailer Yii::$app->mailer yii\swiftmailer\Mailer
Redis Yii::$app->redis yii\redis\Connection
Request Yii::$app->request yii\console\Request
yii\web\Request
Response Yii::$app->response yii\console\Response
yii\web\Response
Router Yii::$app->urlManager yii\web\UrlManager
Security Yii::$app->security yii\base\Security
Session Yii::$app->session yii\web\Session
Url Yii::$app->urlManager yii\web\UrlManager
User Yii::$app->user yii\web\User
View Yii::$app->view yii\web\View

Helpers

Some facades also contain useful helpers to make a development more quick and elegant.

Cache

cache

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

Retrieves a value using the provided key or the specified default value if the value is not cached. If the value is not in the cache, it will be cached. The default value can also be a closure:

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

get

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

Retrieves a value using the provided key and returns it or the specified default value which can also be a closure:

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

Response

bare

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

Returns an empty response with optional headers:

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

html

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

Returns a HTML response with optional headers:

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

json

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

Returns a JSON response with optional headers:

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

jsonp

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

Returns a JSONP response with optional headers:

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

raw

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

Returns a response with data "as is" with optional headers:

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

xml

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

Returns a XML response with optional headers:

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

Extending

If you want a new facade, it's fast and easy, imagine you want to bring a YourFacadeName facade:

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

Then whenever you call

YourFacadeName::hello('world');

it will be executed as

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

jimchen/yii2-facades 适用场景与选型建议

jimchen/yii2-facades 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 380 次下载、GitHub Stars 达 4, 最近一次更新时间为 2020 年 10 月 11 日, 在 PHP 生态内属于活跃度较高的组件。

我们在过去多个企业项目中使用过 jimchen/yii2-facades 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。

围绕 jimchen/yii2-facades 我们能提供哪些服务?
定制开发 / 二次开发

基于 jimchen/yii2-facades 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。

BUG 修复 & 性能优化

线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。

项目外包 & 长期维护

承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。

yvsm@zunyunkeji.com QQ:316430983 微信:yvsm316 西安尊云信息科技 · 专注 PHP / Go / 分布式系统研发

统计信息

  • 总下载量: 380
  • 月度下载量: 0
  • 日度下载量: 0
  • 收藏数: 4
  • 点击次数: 0
  • 依赖项目数: 0
  • 推荐数: 0

GitHub 信息

  • Stars: 4
  • Watchers: 1
  • Forks: 0
  • 开发语言: PHP

其他信息

  • 授权协议: MIT
  • 更新时间: 2020-10-11