yii2intl-tel-input

Getting intl-tel-input working as yii2 autoloader via composer


I'm using Yii2 and would like to be able to use jackocnr/intl-tel-input libs as a autoloader/widget via composer. I can install via composer, but there's no autoload/psr-4 statement in the composer file.

I've used borales/yii2-phone-input in Yii2 and it works well. Do I need to write my own package that uses jackocnr/intl-tel-input?

The other alternative is to just call the libs manually, but would like to void that if possible.

Thanks in advance for any help you can give.


Solution

  • The composer autoloader is for PHP classes but package jackocnr/intl-tel-input contains only js, css and images. There is no php class that composer autoloader can load.

    What you need to do instead is to create an asset bundle that will load the css/js and use that.

    The asset bundle can look for example like this:

    namespace app\assets;
    
    use yii\web\AssetBundle;
    
    class IntlTelInputAsset extends AssetBundle
    {
        public $sourcePath = '@vendor/jackocnr/intl-tel-input/build';
        public $css = [
            'css/intlTelInput.css',
        ];
        public $js = [
            'js/intlTelInput.js',
        ];
    }
    

    Then in view template where you need it you can register the asset:

    \app\assets\IntlTelInputAsset::register($this);
    

    You can learn more about asset bundles in documentation.