Yii2 and Composer

Hello All,

How do I get Yii to load a composer package?

I have a composer package which is not a formal extension of Yii but which I use a lot (in my previous website). I have “required” the package and it is installed in the root vendor folder but I don’t know how to get Yii to recognise it.

Out of interest, composer seems to load files all over the place and I don’t fully understand why. Here is the structure:

[list=1]

[*]Composer.phar is located here (my composer alias also points here): /home/<username>/.composer/composer.phar

[*]Cached files go here: /home/<username>/.composer/cache/files

[*]Composer.json is here: /home/<username>/composer.json

[*]The NON-Yii vendor directory is here: /home/<username>/vendor

[*]The Yii vendor directory is here: /home/<username>/public_html/basic/vendor

[/list]

I notice that swiftmailer is inside /home/<username>/public_html/basic/vendor whereas my new download is inside /home/<username>/vendor.

Any help is explaining how to get Yii to load the new file is most appreciated.

Thanks

OK… I believe I have solved this. Hopefully this will help others in the future.

What I needed to do was formally specify the location of the Yii Vendor directory within the composer.json file. Composer seems to get confused about which vendor directory it is using. My composer.json file - located in /home/<username> - now looks like this:

{

"config": {

&quot;vendor-dir&quot;: &quot;public_html/basic/vendor&quot;

},

"require": {

&quot;yiisoft/yii2-debug&quot;: &quot;^2.0&quot;,


&quot;twig/twig&quot;: &quot;^1.24&quot;,


&quot;alexandernst/yii2-device-detect&quot;: &quot;^0.0.11&quot;

},

"require-dev": {

&quot;yiisoft/yii2-gii&quot;: &quot;^2.0&quot;

}

}

When I ran composer update after modifying the Vendor path, it copied all my files into the Yii Vendor directory and updated the autoloaders.

I then modified the config/web.php as follows:

  1. I added ‘devicedetect’ to bootstrap

  2. I added the following to components:

    'devicedetect' =&gt; [
    
    
         'class' =&gt; 'alexandernst&#092;devicedetect&#092;DeviceDetect'
    
    
     ],
    

My web.php file now looks like this:

'bootstrap' =&gt; [


    'log',


    'devicedetect'


],


'components' =&gt; [


    'devicedetect' =&gt; [


        'class' =&gt; 'alexandernst&#092;devicedetect&#092;DeviceDetect'


    ],

Within one of my model files I called the class as follows:

Yii::&#036;app-&gt;params['devicedetect']

This code:

echo “<pre>”.print_r(Yii::$app->params[‘devicedetect’],1)."</pre>";

produced this output:

Array

(

[isMobile] =&gt; 


[isTablet] =&gt; 


[isDesktop] =&gt; 1

)

I am not sure how well this will work with every composer file but it has worked perfectly on this occasion. At least the files are now downloading to the correct vendor directory.