Turn your widget into Yii2 extension

It’s quite clear itself with one exception, which will be explained later: “autoload” section.

5. Change namespaces

Namespaces before (Poll.php)

namespace frontend\widgets\poll;

use frontend\widgets\poll\models\Polls;
use frontend\widgets\poll\models\PollsResult;
use frontend\widgets\poll\models\PollsResultSearch;
use Yii;
use yii\base\Widget;

Namespaces after

namespace lslsoft\poll;

use lslsoft\poll\models\Polls;
use lslsoft\poll\models\PollsResult;
use lslsoft\poll\models\PollsResultSearch;
use Yii;
use yii\base\Widget;

6. Change all namespaces in all files of the widget

7. Make a record in the vendor\yiisoft\extensions.php. Add an array about extension

'lslsoft/yii2-poll' => 
  array (
    'name' => 'lslsoft/yii2-poll',
    'version' => '9999999-dev',
    'alias' => 
    array (
      '@lslsoft/poll' => $vendorDir . '/lslsoft/yii2-poll',
    ),
  ),

So, ‘version’ – it’s a shame of course, but it will be enough shame in the first time. Don’t worry.
Pay attention on alias

'@lslsoft/poll' => $vendorDir . '/lslsoft/yii2-poll',

Remember that we have actual path “\lslsoft\yii2-poll”, but in name spaces “lslsoft\poll”. That string is responsible for magic. And for magic to occur is responsible
section

"autoload": {
        "psr-4": {
            "lslsoft\\poll\\": ""
        }

in our composer.json
( For more information: http://www.php-fig.org/psr/psr-4/)

8. Change use statement for our widget — so now we use our extension

use lslsoft\poll\Poll;

The string

<?= Poll::widget(); ?>

remained unchanged.

9. Check whether it works properly. If not – fix the bugs.
10. Create a github archive for our extension (in this case — ‘lslsoft\poll’)

11. Push our extension on github

12. Create account on packagist.org and set connection between github archive for our extension.

13. Add a string in “the main” composer.json for all project. Not composer.json we’ve created in lslsoft\yii2-poll. Ih the “require” section

"lslsoft/yii2-poll" :"*"

14. Delete folder “lslsoft\yii2-poll”
15. In Terminal change directory for main directory of you project (where composer.phar is) and type the command :

php composer.phar update

16. Check, whether the folder ‘lslsoft\yii2-poll’ was created
17. Check whether you extension works properly.

If so – problem solved!

Leave a Reply

Your email address will not be published. Required fields are marked *