ptheofan / yii2-hosts-component
A Yii2 component to help you manage domains and hosts throughout your app accross multiple environments.
Installs: 30
Dependents: 0
Suggesters: 0
Security: 0
Stars: 0
Watchers: 1
Forks: 0
Open Issues: 0
Type:yii2-extension
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-11-29 06:35:33 UTC
README
Easily manage your domains and subdomains across multiple environments
In your config (example /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 ], ], ], ],
Then you can access the following
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
To support autocomplete you have two options
- Extend the Hosts class with your own component and add @property annotations
- Create a mock file and mark the original file as text-only (if your IDE supports it). Then add the @property annotations you want to the class.
Here's a simple example of the first method
// @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)