yii2-facades

Facades for Yii 2

Github星跟蹤圖

Facades for Yii 2

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.

Code Quality Build Status Code Coverage SensioLabsInsight

Packagist Version Total Downloads Software License

Table of contents

Installation

The preferred way to install this extension is through composer.

Either run

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

or add

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

to the require section of your composer.json file.

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 sergeymakinen\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

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');

主要指標

概覽
名稱與所有者sergeymakinen/yii2-facades
主編程語言PHP
編程語言PHP (語言數: 1)
平台
許可證MIT License
所有者活动
創建於2016-08-02 20:37:00
推送於2019-09-19 18:06:53
最后一次提交2019-09-19 21:06:41
發布數13
最新版本名稱v1.5.3 (發布於 )
第一版名稱v1.0.0 (發布於 )
用户参与
星數22
關注者數3
派生數2
提交數52
已啟用問題?
問題數0
打開的問題數0
拉請求數1
打開的拉請求數0
關閉的拉請求數0
项目设置
已啟用Wiki?
已存檔?
是復刻?
已鎖定?
是鏡像?
是私有?