I've created several useful commands, which are useful for us who develop Magento 2 sites on a daily basis. Magento 2 console commands are based on symphony, you can create commands for your personal/team use, something like bin/magento cache:clean
. This way you can execute the command directly from the terminal.
Here is a simple hello world command. Before we get started clear you generation folder and here is what you need to do.
Create a new module for illustration purposes I'll call it Tools
under app/code/Andre/
, include the registration.php
and module.xml
.
app/code/Andre/Tools/registration.php
\Magento\Framework\Component\ComponentRegistrar::register(
\Magento\Framework\Component\ComponentRegistrar::MODULE,
'Andre_Tools',
__DIR__
);
app/code/Andre/Tools/etc/module.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:Module/etc/module.xsd">
<module name="Andre_Tools" setup_version="0.1.0"/>
</config>
Create a new model clas, this is where it will contains the options, description and the logic of your command.
app/code/Andre/Tools/Model/Generation.php
namespace Andre\Tools\Model;
use \Symfony\Component\Console\Command\Command;
use \Symfony\Component\Console\Input\InputInterface;
use \Symfony\Component\Console\Output\OutputInterface;
class Generation extends Command
{
protected function configure()
{
$this->setName('generation:clean')
->setDescription('The description of you command here!');
parent::configure();
}
protected function execute(InputInterface $input, OutputInterface $output)
{
$output->writeln('Hello World!');
}
}
app/code/Andre/Tools/etc/di.xml
<?xml version="1.0"?>
<config xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:noNamespaceSchemaLocation="urn:magento:framework:ObjectManager/etc/config.xsd">
<type name="Magento\Framework\Console\CommandList">
<arguments>
<argument name="commands" xsi:type="array">
<item name="clean" xsi:type="object">Andre\Tools\Model\Generation</item>
</argument>
</arguments>
</type>
</config>
Lastly do a bin/magento setup:upgrade
, check that the module is active bin/magento module:status
if not then run bin/magento module:enable Andre_Tools
.
Now to run the command you just create simply run:
bin/magento generation:clean
Now just add your logic under the execute()
method to delete the generation folder. It shouldn't be hard to delete a folder with PHP.
please check your project folder,
if you don't see the class file maybe it's deleted or there is something wrong in your magento extension installation
if the class file exist, I think it could be 3rd extension that try inject that class. you can checking again 3rd extension that already install in your magento project
Best Answer
yes, if you just run
di:compile
command, then it will override you current generated folder, it will delete all previous files and create new one.But your directory need to have permission to delete through command, and also if files are currently in used (Through browser or anywhere else) it will show you error. But most of the time it will just delete old files and create new one in generated.
I got issues in my localhost, it will not delete files in generated, and shows error, but on my server (Ubuntu) it will work perfectly, so, I don't need to delete generated directory before running command