赵天帆/yii2-hosts-component

一个用于帮助您在多个环境中管理应用程序的域名和主机的 Yii2 组件。

安装: 30

依赖: 0

建议者: 0

安全性: 0

星标: 0

关注者: 1

分支: 0

开放问题: 0

类型:yii2-extension

v1.0.1 2020-12-31 11:39 UTC

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

为了支持自动完成,您有两个选择

  1. 通过您自己的组件扩展 Hosts 类并添加 @property 注释
  2. 创建一个模拟文件,并将原始文件标记为纯文本(如果您的 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)