One of the most common questions in the Magento 2 community is why they should run the setup:di:compile command in Magento.
And the answer is simple. - If you think that your compilation process takes much more time than it should, you need to run the setup:di:compile command.
What Does the setup:di:compile Command Do in Magento?
php bin/magento setup:di:compile
The above setup:di:compile command basically generates the contents of var/di folder in Magento < 2.2 and for >=2.2.
According to the official Magento docs, it compiles code like Factories, Proxies, Interceptors, etc. and puts them in generated or var/generation directory.
Here are a couple of things that will happen when you run the setup:di:compile command:
- Area Configuration Aggregation - It will optimize the dependency injection configurations according to the area.
- Application Code Generation - for Proxies, Factories, Plugins, etc.
- Interceptor Generation - It will optimize the code generation of interceptors
- Interceptor Cache Generation - It will optimize the cache generation of interceptors.
- Service Data Attributes Generation - It will generate extension classes for the data objects.
- Repositories Code Generation - It will simply generate the necessary code for APIs.
When Should You Run setup:di:compile Command in Magento?
Like we said earlier, if the compilation process starts taking more time than it should, it is likely that you need to run setup:di:compile command.
The compilation process basically slows down when developers make lots of changes in proxy js, factory methods, or just about any code.
So, if you believe you’ve made an abundance of changes in your project and seeing a slow down in compilation process speed, it’s probably time to execute setup:di:compile command.
However, you can also use the setup:di:compile command for a specific module. Below is the step-by-step process you need to follow to run the command for the specific module.
How to Run setup:di:compile Command for Specific Module in Magento 2
Here are the 3 steps you need to follow to run setup:di:compile for a specific module.
Step 1:
Go to /var/www/html/yourMagento/setup/src/Magento/Setup/Console/Command/DiCompileCommand.php
Step 2:
Open the file and locate the below-mentioned line:
$modulePaths = $this->componentRegistrar->getPaths(ComponentRegistrar::MODULE);
Step 3:
Lastly, replace the below-mentioned line and add the path of the module that you want to compile as shown below:
$modulePaths = ['CompanyName_ModuleName' => '/var/www/html/yourMagento/app/code/CompanyName/ModuleName'];
IMPORTANT NOTE: Before you execute the command for a specific module, make sure that the module registers with this CompanyName_ModuleName in registration.php file.
Also, it’s a temporary solution for DI compile for a specific module.
So, remember to remove the di generated module folder and refresh the cache.
Also read: Useful Magento 2 Command List for Beginners
Concluding Thoughts…
We hope you find it useful and in case you face any difficulties and need our professional help with Magento 2 Developer, we're always happy to assist.