Yii 2アプリケーションコンポーネントのFacadesサポートのLaravel。あなたが望むもの:シンプルで広範囲で、PHPDOCを介したIDEオート完成サポートを備えているので、失望することはありません。
この拡張機能をインストールするための好ましい方法は、Composerを通じてです。
どちらか走ってください
composer require " sergeymakinen/yii2-facades:^1.0 "または追加します
"sergeymakinen/yii2-facades" : " ^1.0 " composer.jsonファイルの要求セクションに。
基本的に、拡張機能をインストールし、すべての通常のYii 2アプリケーションコンポーネントで行うように使用を開始しますが、より短いシンプルなFacadeを使用して、見sergeymakinenfacadesみましょう。
前に:
$ 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 )| 名前 | Facadedコンポーネントエイリアス | コンポーネント/インターフェイス |
|---|---|---|
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 ' );