Ecosyste.ms: Awesome
An open API service indexing awesome lists of open source software.
https://github.com/sergeymakinen/yii2-facades
Facades for Yii 2
https://github.com/sergeymakinen/yii2-facades
closure facade facades laravel php php-library yii yii2 yii2-extension
Last synced: 6 days ago
JSON representation
Facades for Yii 2
- Host: GitHub
- URL: https://github.com/sergeymakinen/yii2-facades
- Owner: sergeymakinen
- License: mit
- Archived: true
- Created: 2016-08-02T20:37:00.000Z (over 8 years ago)
- Default Branch: master
- Last Pushed: 2019-09-19T18:06:53.000Z (over 5 years ago)
- Last Synced: 2025-01-20T21:09:50.385Z (6 days ago)
- Topics: closure, facade, facades, laravel, php, php-library, yii, yii2, yii2-extension
- Language: PHP
- Size: 128 KB
- Stars: 22
- Watchers: 3
- Forks: 2
- Open Issues: 0
-
Metadata Files:
- Readme: README.md
- License: LICENSE
- Code of conduct: CODE_OF_CONDUCT.md
Awesome Lists containing this project
README
# 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](https://www.phpdoc.org) so you won't be disappointed.
[![Code Quality](https://img.shields.io/scrutinizer/g/sergeymakinen/yii2-facades.svg?style=flat-square)](https://scrutinizer-ci.com/g/sergeymakinen/yii2-facades) [![Build Status](https://img.shields.io/travis/sergeymakinen/yii2-facades.svg?style=flat-square)](https://travis-ci.org/sergeymakinen/yii2-facades) [![Code Coverage](https://img.shields.io/codecov/c/github/sergeymakinen/yii2-facades.svg?style=flat-square)](https://codecov.io/gh/sergeymakinen/yii2-facades) [![SensioLabsInsight](https://img.shields.io/sensiolabs/i/3d6f8afa-4baf-40df-9923-2053ece59b7c.svg?style=flat-square)](https://insight.sensiolabs.com/projects/3d6f8afa-4baf-40df-9923-2053ece59b7c)
[![Packagist Version](https://img.shields.io/packagist/v/sergeymakinen/yii2-facades.svg?style=flat-square)](https://packagist.org/packages/sergeymakinen/yii2-facades) [![Total Downloads](https://img.shields.io/packagist/dt/sergeymakinen/yii2-facades.svg?style=flat-square)](https://packagist.org/packages/sergeymakinen/yii2-facades) [![Software License](https://img.shields.io/badge/license-MIT-brightgreen.svg?style=flat-square)](LICENSE)
## Table of contents
- [Installation](#installation)
- [Usage](#usage)
* [Generate random string](#generate-random-string)
* [Fetch all users (just an example!)](#fetch-all-users-just-an-example)
* [Format currency](#format-currency)
- [Access properties](#access-properties)
- [Available facades](#available-facades)
- [Helpers](#helpers)
* [Cache](#cache)
+ [cache](#cache)
+ [get](#get)
* [Response](#response)
+ [bare](#bare)
+ [html](#html)
+ [json](#json)
+ [jsonp](#jsonp)
+ [raw](#raw)
+ [xml](#xml)
- [Extending](#extending)## Installation
The preferred way to install this extension is through [composer](https://getcomposer.org/download/).
Either run
```bash
composer require "sergeymakinen/yii2-facades:^1.0"
```or add
```json
"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:
```php
$random = Yii::$app->security->generateRandomString(128);
```After:
```php
$random = Security::generateRandomString(128);
```### Fetch all users (just an example!)
Before:
```php
$users = Yii::$app->db->createCommand('SELECT * FROM users;')->queryAll();
```After:
```php
$users = Db::createCommand('SELECT * FROM users;')->queryAll();
```### Format currency
Before:
```php
$price = Yii::$app->formatter->asCurrency(123456.78, 'USD');
```After:
```php
$price = Formatter::asCurrency(123456.78, 'USD');
```## Access properties
Any class public property `$foo` can be got via an accessor:
```php
$value = YourFacadeName::getFoo()
```
And set:```php
YourFacadeName::setFoo($value)
```
## Available facadesName
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
```php
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:
```php
$users = Cache::cache('users', function () {
return app\models\Users::findAll();
}, 3600);
```#### get
```php
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:
```php
$options = Cache::get('options', function () {
return [
'option1' => false,
'option2' => true
];
});
```### Response
#### bare
```php
public static function bare($statusCode = 204, array $headers = [])
```Returns an empty response with optional headers:
```php
public function actionCreate()
{
// ...
return Response::bare(201);
}
```#### html
```php
public static function html($data, array $headers = [])
```Returns a HTML response with optional headers:
```php
public function actionIndex()
{
// ...
return Response::html($this->render('index'), [
'Cache-Control' => 'no-cache'
]);
}
```#### json
```php
public static function json($data, array $headers = [])
```Returns a JSON response with optional headers:
```php
public function actionList()
{
// ...
return Response::json(Db::createCommand('SELECT * FROM users')->all());
}
```#### jsonp
```php
public static function jsonp($data, $callback = 'callback', array $headers = [])
```Returns a JSONP response with optional headers:
```php
public function actionApi($callback)
{
// ...
return Response::jsonp([
'success' => true,
'response' => $data
], $callback);
}
```#### raw
```php
public static function raw($data, array $headers = [])
```Returns a response with data "as is" with optional headers:
```php
public function actionCreate()
{
// ...
return Response::raw($binary, [
'Content-Type' => 'application/octet-stream'
]);
}
```#### xml
```php
public static function xml($data, array $headers = [])
```Returns a XML response with optional headers:
```php
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:
```php
class YourFacadeName extends Facade
{
/**
* @inheritdoc
*/
public static function getFacadeComponentId()
{
return 'yourFacadeComponentName'; // Yii::$app->yourFacadeComponentName
}
}
```Then whenever you call
```php
YourFacadeName::hello('world');
```it will be executed as
```php
Yii::$app->get('yourFacadeComponentName')->hello('world');
```