If you plan to reuse functionality, it is recommended to provide the code in composer packages. Luckily, Yii 2.0 Framework is equipped with a code generator for creating ready to use extension skeletons in a minute.
Note! Code generation is only available with
To create an extension from the command line adjust the parameters in the following command and run it from your project root.
./yii gii/extension \
--title="Extension for Yii 2.0 Framework" \
--description="Provides cool stuff" \
--authorName="Your Name" \
This will generate a folder in
runtime/tmp-extensions/yii2-mypackage with example code for the extension.
Create a new private or public repository, eg. on GitHub.
Then, go to the folder with the generated code inside your project's
runtime folder and initializie a temporary
git add .
Check if you have added the correct files with
It should show three files to be committed. If everything is fine commit and push your changes to your repo.
git commit -m "initial commit"
git remote add origin https://github.com/mycompany/yii2-mypackage.git
git push origin master
This is a one-time push from this repo, you should install your extension via composer now.
If you are developing packages for a private repository you can enable your package by adding it's repository URL to
You should use the git protocol when using private repositories in conjunction with private-public-key authentication when deploying remotely or in a virtualized environment. You can add
To make your extension available publicly, submit it to packagist.
It should be available after a few minutes, if you are heavily developing an extension you can still add it's repository
composer.json, like described above. Composer will then check the repository on every update and you'll
get always the latest commits.
Now get the code in any Yii2 application with
composer require mycompany/yii2-mypackage:dev-master
Depending on your extension and its bootstrapping configuration you may have to configure it in the application configuration. The classes from your package are available via auto-loading and accessible by their namespace.
You can also use the Gii web-interface and your favorite Git UI client to accomplish the tasks described above.
Gii > Extension Generator
Set constraint to
@dev before starting code modifications and run
composer require vendor/package:@dev
If you've already changed code in a dist-package, you can move away the package with the changes.
mv package _package, run the above command and then copy only the contents from the modified folder into the package folder.
git statusshould now your changes now.
Update configuration to get sources for the forked package
See also https://github.com/dmstr-forks
Help us to improve the documentation, fork this page.