PHP code example of ptheofan / yii2-hosts-component

1. Go to this page and download the library: Download ptheofan/yii2-hosts-component library. Choose the download type require.

2. Extract the ZIP file and open the index.php.

3. Add this code to the index.php.
    
        
<?php
require_once('vendor/autoload.php');

/* Start to develop here. Best regards https://php-download.com/ */

    

ptheofan / yii2-hosts-component example snippets


'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

// @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)