2017 © Pedro Peláez
 

yii2-extension yii2-facades

Laravel like facades support for Yii 2 application components

image

sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  • Friday, June 16, 2017
  • by sergeymakinen
  • Repository
  • 2 Watchers
  • 17 Stars
  • 669 Installations
  • PHP
  • 1 Dependents
  • 0 Suggesters
  • 3 Forks
  • 0 Open issues
  • 14 Versions
  • 19 % Grown

The README.md

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., (*1)

Code Quality Build Status Code Coverage SensioLabsInsight, (*2)

Packagist Version Total Downloads Software License, (*3)

Table of contents

Installation

The preferred way to install this extension is through composer., (*4)

Either run, (*5)

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

or add, (*6)

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

to the require section of your composer.json file., (*7)

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):, (*8)

Generate random string

Before:, (*9)

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

After:, (*10)

$random = Security::generateRandomString(128);

Fetch all users (just an example!)

Before:, (*11)

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

After:, (*12)

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

Format currency

Before:, (*13)

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

After:, (*14)

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

Access properties

Any class public property $foo can be got via an accessor:, (*15)

$value = YourFacadeName::getFoo()

And set:, (*16)

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
Http Yii::$app->httpClient yii\httpclient\Client
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., (*17)

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:, (*18)

$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:, (*19)

$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:, (*20)

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

html

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

Returns a HTML response with optional headers:, (*21)

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:, (*22)

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:, (*23)

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:, (*24)

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:, (*25)

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:, (*26)

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

Then whenever you call, (*27)

YourFacadeName::hello('world');

it will be executed as, (*28)

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

The Versions

16/06 2017

dev-master

9999999-dev https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel yii2 yii facade facades yii2-facades

13/06 2017

v1.5.3

1.5.3.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel yii2 yii facade facades yii2-facades

01/03 2017

v1.5.2

1.5.2.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel yii2 yii facade facades yii2-facades

24/11 2016

v1.5.1

1.5.1.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

The Development Requires

laravel yii2 yii facade facades yii2-facades

21/10 2016

v1.5.0

1.5.0.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

03/10 2016

v1.4.0

1.4.0.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

31/08 2016

v1.3.2

1.3.2.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

23/08 2016

v1.3.1

1.3.1.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

23/08 2016

v1.3.0

1.3.0.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

17/08 2016

v1.2.0

1.2.0.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

11/08 2016

v1.1.2

1.1.2.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

07/08 2016

v1.1.1

1.1.1.0 https://github.com/sergeymakinen/yii2-facades

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

03/08 2016

v1.1.0

1.1.0.0

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades

02/08 2016

v1.0.0

1.0.0.0

Laravel like facades support for Yii 2 application components

  Sources   Download

MIT

The Requires

 

laravel yii2 yii facade facades yii2-facades