Best practices when making a Yii2 Extension

What is the best way to create a extension for Yii2?

How to register the extension with the application and how to organize views, controllers and models within the extension?


You can follow each step below to help you set-up your first Yii2 extension:

1. Github setup

Create a new github repo and make sure to follow the naming conventions for yii2-extensions like this:


ourcompany being our github-scope and ourextension the name of our extension.


2. Local project setup

Create a local folder where you will develop the extension. First create a file composer.json. This folder should not be in the vendor-folder of an existing project! It won't work any other way, except to declare a namespaces within autoload.php of surrounding project manually.


The most important things to remember:

  • The dependencies (usually the Yii2-framework)
  • Autoload-property which declares the namespace for package. This will later be used by yiis-autoloader to find classes.

After that, run composer update this will create the vendor folder containing all referenced libraries.

Here is an example of composer:

Pay special attention to the autoload block at the end of the file! In this case autoloader that asinfotrack/yii2/toolbox will be mapped to the root-folder of the project. Folder models with class in your project root should have the namespace asinfotrack/yii2/toolbox/models.


The next to create .gitignore-file. Exclude the vendor-folder and all IDE files.

Also an example of the same extension mentioned above. Simply copy/paste it:


Now initialize the git-repo, simply navigate to directory and execute the command git init. To add all files and commit for the first time, execute git add -A and git commit -m "message here". Now you are ready to push repo to github.

Need to set the remote of repo to github. This is done with the following command:

git remote add origin

Also make sure that the following config vars are set correctly:

git config git config

The email should match with your github-account email. If you want to change the values, simply append them to the commands above. Like below:

git config ""

Now we need to push the extension to github:

git push origin master


3. Packagist-setup

For other users to be able to reference our extension within their projects, we need to register it with

Create an account and submit extension as explained here:


4. Connecting packagist and github

Packagist mirrors repo for further distribution. Enabling the Packagist service hook ensures that your package will always be updated instantly when you push to GitHub.

Go to the settings of github-repo and navigate to Webhooks & Services. Then add a new hook for packagist. This is supported natively by github. Find the packagist-hook in the dropdown.

We need to provide packagist-API-Token which we can find on the top of account-page when logged into Everything is documented very well here:


5. Conclusion

That's it! When performing a new commit to github, it will be mirrored automatically to packagist. With a few minutes delay we will receive the updated version of our extension when we execute composer update.

At first all of this might look intimidating and complicated, but actually it's pretty straight forward.


Was this article helpful?

Top Related Jobs

Top Freelance jobs are waiting for you, View & apply to earn more.

    No results found.

People also searched for

Learn Web Scraping using Python
Web Development

The importance of extracting information from the web is becoming increasingly loud and clear. Every few weeks, I realize myself in a situation where we need to extract information...

Read More
Natural Language Processing in Python
Natural Language Processing in Python
Web Development

NLP is a branch of data science that consists of systematic processes for analyzing, understanding, and deriving information from the text information in a smart and efficient mann...

Read More
How to post a Job & Hire a great WordPress developer
How to post a Job & Hire a great WordPress develop...
Web Development

WordPress is an open source Content Management System (CMS), which allows the users to build dynamic websites and blog. WordPress is the most popular blogging system on the web and...

Read More

You are freelancing Ninja?

Try Toogit Instant Connect today, The new virtual assistant for your freelancing world.

What are the benefits to hire a php developer on Toogit?

Hire PHP developer on Toogit to build a website as per the requirement of your business in a convenient manner. A team of professionals can undertake the requirement of your business and can turn your ideas into reality. Hiring PHP developer is a must for you if you want to build a PHP based site with a professional look. Web developers from all over the world are opting for this software to build a site because of its cost effectiveness, flexibility, simplicity and free availability. Hire PHP developers on Toogit to accomplish your business projects.

  • Flexible option: Our multiple models are designed to give you the flexibility in hiring your team at your own budget and project requirements.
  • Build your own team: Choose from a variety of technologies and create your dream team or your own IT team.
  • Transparency at all levels: Options to choose from a pool of talented resources, interviewing them as per your own standards and hiring the right resource.
  • Complete control: Assign tasks at your own will or give the control to the team to lead the development.

What are php developers?

PHP developers develop programs, applications, and web sites using the dynamic scripting language PHP. PHP is known for web development and business applications. Depending on job function, PHP developers could also be classified as software developers or web developers. Because the language is so ubiquitous in web site development and business, PHP can be a good choice for freelance or contract developers.

Mistakes while hiring the PHP Developers

Common mistake that need to be avoided while hiring a PHP developer

  1. Not examining the previous work effectively: Basically, you have to judge the work of the PHP Developer when you are trying to hire them. Of course, you'll have some idea of the skills that are required for the task and some assumptions on how you would like to proceed additional.
  2. Looking within the region: 'I would prefer to hire a PHP Developer who belongs to my region' this can be the fact that the majority of the businesses think and work in this direction. Well, most of them think that this the simplest way; communication becomes straightforward, problems may be solved with ease and at a faster rate.
  3. Ignoring support and communication: Make sure that your communication is perfect. A proper mode of communication should be selected by the company for solving and reviewing the latest updates and problems. So, while interviewing, test their analytical, decision-making, and problem-solving ability, and also check whether the developer is able to understand and implement the requirements in the project easily or not.
  4. Pricing issue: Do you agree on variable pricing? You never know how much money are going to be spent throughout the process and how many months it would take for the website to go live. This problem arises when working with an offshore. So, in the first interview itself, talk about all the issues clearly. Make sure you provide the detailed contract which is duly signed by the offshore company.

What our users are discussing about Yii2