赵天帆 / yii2-hosts-component
一个用于帮助您在多个环境中管理应用程序的域名和主机的 Yii2 组件。
v1.0.1
2020-12-31 11:39 UTC
Requires
- php: >=7.4.0
- yiisoft/yii2: ^2.0
Suggests
- bazilio/yii2-stubs-generator: Universal IDE Autocomplete by generating stubs based on your configs
- ptheofan/yii2-file-upload-behavior: A highly customisable system to make file-uploads management as trivial as it should be.
This package is auto-updated.
Last update: 2024-08-29 05:37:18 UTC
README
轻松管理多个环境中的域名和子域名
在您的配置文件中(例如 /common/main-local.php
)
'components' => [ 'host' => [ 'class' => \ptheofan\components\Hosts::class, 'config' => [ 'storage' => [ 'http' => false, // set to false by default 'https' => true, // this is by default set to true 'hostname' => 'storage.example.com', // the domain of interest ], ], ], ],
然后您可以访问以下内容
echo Yii::$app->host->storage->url(); // will return https://storage.example.com/ echo Yii::$app->host->storage->url('/avatars'); // will return https://storage.example.com/avatars echo Yii::$app->host->storage->url('/avatars/avatar.png'); // will return https://storage.example.com/avatars/avatar.png
为了支持自动完成,您有两个选择
- 通过您自己的组件扩展 Hosts 类并添加 @property 注释
- 创建一个模拟文件,并将原始文件标记为纯文本(如果您的 IDE 支持)。然后,将您想要添加到类的 @property 注释添加到其中。
下面是第一种方法的简单示例
// @file /common/components/Hosts.php /** * @property Host $storage */ class Hosts extends \ptheofan\components\Hosts { } // @file /common/config/main-local.php 'components' => [ 'host' => [ 'class' => \common\components\Hosts::class, 'config' => [ 'storage' => [ 'http' => false, // set to false by default 'https' => true, // this is by default set to true 'hostname' => 'storage.example.com', // the domain of interest ], ], ], ], // if you have properly configured the stubs for autocomplete then you should get autocomplete when you write Yii::$app->host-> // (autocomplete here all the Hosts annnotated @properties)