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 ' );