yii2 facades
v1.5.3
Laravel喜歡外牆支持YII 2應用程序組件。正是您想要的:簡單,廣泛且通過PHPDOC提供IDE自動完成支持,因此您不會失望。
安裝此擴展程序的首選方法是通過作曲家。
要么運行
composer require " sergeymakinen/yii2-facades:^1.0 "或添加
"sergeymakinen/yii2-facades" : " ^1.0 "要為composer.json文件的要求部分。
基本上,您安裝了擴展程序並開始像所有普通的YII 2應用程序組件一樣開始使用它,但是使用較短的簡單語法,讓我們看一下(如果您想知道所有默認的立面,包括Sergeymakinen achememakinen Facade sergeymakinenfacades namepace的所有默認立面):
前:
$ random = Yii:: $ app -> security -> generateRandomString ( 128 );後:
$ random = Security:: generateRandomString ( 128 );前:
$ users = Yii:: $ app -> db -> createCommand ( ' SELECT * FROM users; ' )-> queryAll ();後:
$ users = Db:: createCommand ( ' SELECT * FROM users; ' )-> queryAll ();前:
$ price = Yii:: $ app -> formatter -> asCurrency ( 123456.78 , ' USD ' );後:
$ price = Formatter:: asCurrency ( 123456.78 , ' USD ' );任何類公共財產$foo都可以通過登錄器獲得:
$ value = YourFacadeName:: getFoo ()並設置:
YourFacadeName:: setFoo ( $ value )| 姓名 | 面部成分別名 | 組件/接口 |
|---|---|---|
Asset | Yii::$app->assetManager | yiiwebAssetManager |
Auth | Yii::$app->auth | yiirbacManagerInterface |
Cache | Yii::$app->cache | yiicachingCache |
Db | Yii::$app->db | yiidbConnection |
Error | Yii::$app->errorHandler | yiiconsoleErrorHandler |
yiiwebErrorHandler | ||
Formatter | Yii::$app->formatter | yiii18nFormatter |
Http | Yii::$app->httpClient | yiihttpclientClient |
I18n | Yii::$app->i18n | yiii18nI18N |
Log | Yii::$app->log | yiilogDispatcher |
Mailer | Yii::$app->mailer | yiiswiftmailerMailer |
Redis | Yii::$app->redis | yiiredisConnection |
Request | Yii::$app->request | yiiconsoleRequest |
yiiwebRequest | ||
Response | Yii::$app->response | yiiconsoleResponse |
yiiwebResponse | ||
Router | Yii::$app->urlManager | yiiwebUrlManager |
Security | Yii::$app->security | yiibaseSecurity |
Session | Yii::$app->session | yiiwebSession |
Url | Yii::$app->urlManager | yiiwebUrlManager |
User | Yii::$app->user | yiiwebUser |
View | Yii::$app->view | yiiwebView |
一些立面還包含有用的幫助者,以使開發更快,優雅。
public static function cache( $ key , $ default , $ duration = 0 , $ dependency = null )如果未緩存該值,則使用提供的密鑰或指定的默認值檢索值。如果該值不在緩存中,則將被緩存。默認值也可以是一個關閉:
$ users = Cache:: cache ( ' users ' , function () {
return app models Users:: findAll ();
}, 3600 ); public static function get( $ key , $ default = false )使用提供的密鑰檢索值並返回它或指定的默認值,這也可能是關閉:
$ options = Cache:: get ( ' options ' , function () {
return [
' option1 ' => false ,
' option2 ' => true
];
}); public static function bare( $ statusCode = 204 , array $ headers = [])返回可選標頭空的空響應:
public function actionCreate ()
{
// ...
return Response:: bare ( 201 );
} public static function html( $ data , array $ headers = [])返回帶有可選標頭的HTML響應:
public function actionIndex ()
{
// ...
return Response:: html ( $ this -> render ( ' index ' ), [
' Cache-Control ' => ' no-cache '
]);
} public static function json( $ data , array $ headers = [])帶有可選標題的JSON響應:
public function actionList ()
{
// ...
return Response:: json (Db:: createCommand ( ' SELECT * FROM users ' )-> all ());
} public static function jsonp( $ data , $ callback = ' callback ' , array $ headers = [])通過可選標頭返回JSONP響應:
public function actionApi ( $ callback )
{
// ...
return Response:: jsonp ([
' success ' => true ,
' response ' => $ data
], $ callback );
} public static function raw( $ data , array $ headers = [])帶有可選標題的數據“原樣”返迴響應:
public function actionCreate ()
{
// ...
return Response:: raw ( $ binary , [
' Content-Type ' => ' application/octet-stream '
]);
} public static function xml( $ data , array $ headers = [])返回帶有可選標頭的XML響應:
public function actionCreate ()
{
// ...
return Response:: xml ([
' success ' => true ,
' response ' => $ data
]);
}如果您想要一個新的外牆,它很容易,想像一下,您想帶上一個YourFacadeName立面:
class YourFacadeName extends Facade
{
/**
* @inheritdoc
*/
public static function getFacadeComponentId ()
{
return ' yourFacadeComponentName ' ; // Yii::$app->yourFacadeComponentName
}
}然後每當你打電話
YourFacadeName:: hello ( ' world ' );它將被執行為
Yii:: $ app -> get ( ' yourFacadeComponentName ' )-> hello ( ' world ' );