All Projects → sergeymakinen → yii2-facades

sergeymakinen / yii2-facades

Licence: MIT License
Facades for Yii 2

Programming Languages

PHP
23972 projects - #3 most used programming language

Projects that are alternatives of or similar to yii2-facades

ar-variation
Variation behavior for ActiveRecord
Stars: ✭ 46 (+119.05%)
Mutual labels:  yii2, yii, yii2-extension
ar-role
ActiveRecord behavior, which provides relation roles (table inheritance)
Stars: ✭ 34 (+61.9%)
Mutual labels:  yii2, yii, yii2-extension
Balance
Balance accounting (bookkeeping) system based on debit and credit principle
Stars: ✭ 162 (+671.43%)
Mutual labels:  yii2, yii, yii2-extension
File Storage
File storage abstraction for Yii2
Stars: ✭ 116 (+452.38%)
Mutual labels:  yii2, yii, yii2-extension
ar-search
Provides unified search model for Yii ActiveRecord
Stars: ✭ 31 (+47.62%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Assets Auto Compress
Automatic compilation of js + css + html
Stars: ✭ 147 (+600%)
Mutual labels:  yii2, yii, yii2-extension
yii2-presenter
Yii2 View Presenter
Stars: ✭ 13 (-38.1%)
Mutual labels:  yii2, yii, yii2-extension
Ar Linkmany
ActiveRecord behavior for saving many-to-many relations
Stars: ✭ 83 (+295.24%)
Mutual labels:  yii2, yii, yii2-extension
content
Content management system for Yii2
Stars: ✭ 54 (+157.14%)
Mutual labels:  yii2, yii, yii2-extension
install
basic script for project installation
Stars: ✭ 17 (-19.05%)
Mutual labels:  yii2, yii, yii2-extension
Ar Position
ActiveRecord behavior, which provides ability for custom records order setup
Stars: ✭ 107 (+409.52%)
Mutual labels:  yii2, yii, yii2-extension
yii2-formbuilder
A drag and drop form builder with jQuery for Yii2
Stars: ✭ 33 (+57.14%)
Mutual labels:  yii2, yii, yii2-extension
Admin
Admin pack (actions, widgets, etc) for Yii2
Stars: ✭ 100 (+376.19%)
Mutual labels:  yii2, yii, yii2-extension
filedb
ActiveRecord for static data definitions based on files
Stars: ✭ 72 (+242.86%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Aws S3
An Amazon S3 component for Yii2
Stars: ✭ 86 (+309.52%)
Mutual labels:  yii2, yii, yii2-extension
Crontab
Yii2 extension for crontab support
Stars: ✭ 170 (+709.52%)
Mutual labels:  yii2, yii, yii2-extension
Yii2 Schemadump
Generate the schema from an existing database.
Stars: ✭ 78 (+271.43%)
Mutual labels:  yii2, yii, yii2-extension
Csv Grid
Yii2 extension for CSV export
Stars: ✭ 83 (+295.24%)
Mutual labels:  yii2, yii, yii2-extension
Ar Softdelete
Soft delete behavior for ActiveRecord
Stars: ✭ 188 (+795.24%)
Mutual labels:  yii2, yii, yii2-extension
behavior-trait
Allows handling events via inline declared methods, which can be added by traits
Stars: ✭ 18 (-14.29%)
Mutual labels:  yii2, yii, yii2-extension

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

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.

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');
Note that the project description data, including the texts, logos, images, and/or trademarks, for each open source project belongs to its rightful owner. If you wish to add or remove any projects, please contact us at [email protected].