I've worked in Magento 2 full-time for 5.5 years, and in Laravel on and off since 2015. Put the two side by side, and the console is the easiest place to start: both CLIs are Symfony Console underneath, and they share one trap.
Same component underneath
In Magento 2, Magento\Framework\Console\Cli, the class behind bin/magento, extends Symfony's Console Application. Laravel's Illuminate\Console\Application, the class behind php artisan, extends it too. Every command in both frameworks extends Symfony's Command.
So a command in either framework reads Symfony's input object, writes to Symfony's output object and returns an exit code. Laravel adds 2 things: a signature string that declares the arguments and options, and a handle() method that the container calls. Registration differs too. Magento collects commands from di.xml. Laravel picks up the classes in app/Console/Commands.
The trap they share
When bin/magento starts, it builds every command registered in CommandListInterface, with all of its constructor dependencies, even if all you asked for was cache:flush. On Magento 2.4.9 installed from the GitHub repository, that's 72 command objects per run. One heavy constructor slows down every bin/magento call, the cron run that fires every minute included.
The classic fix is a generated Proxy, wired in di.xml. The heavy service is created only when the command actually runs. The core does this itself in the image-resize command.
<type name="Magento\Framework\Console\CommandListInterface">
<arguments>
<argument name="commands" xsi:type="array">
<item name="acme_export" xsi:type="object">Acme\Export\Console\ExportOrders</item>
</argument>
</arguments>
</type>
<!-- built on every bin/magento run: inject the heavy service lazily -->
<type name="Acme\Export\Console\ExportOrders">
<arguments>
<argument name="exporter" xsi:type="object">Acme\Export\Model\OrderExporter\Proxy</argument>
</arguments>
</type>
class ExportOrders extends Command
{
public function __construct(private OrderExporterInterface $exporter)
{
parent::__construct('acme:export-orders');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln($this->exporter->export() . ' orders exported');
return Cli::RETURN_SUCCESS;
}
}
With the exporter injected directly, bin/magento cache:status builds it on every run. With the Proxy it doesn't, and acme:export-orders still prints "3 orders exported".
Since 2.4.8 there's a cleaner way. Magento\Framework\Console\CommandLoader implements Symfony's lazy CommandLoaderInterface. Register the command there by name, and Magento builds it only when you call it. bin/magento list and a bare bin/magento are the exceptions: both build it too. No Proxy needed, only the usual <preference> for the interface in the constructor.
<preference for="Acme\Export\Api\OrderExporterInterface" type="Acme\Export\Model\OrderExporter"/>
<!-- 2.4.8+: registered by name, built only when called -->
<type name="Magento\Framework\Console\CommandLoader">
<arguments>
<argument name="commands" xsi:type="array">
<item name="acme_export" xsi:type="array">
<item name="name" xsi:type="string">acme:export-orders</item>
<item name="class" xsi:type="string">Acme\Export\Console\ExportOrders</item>
</item>
</argument>
</arguments>
</type>
Laravel has the same split. A command without the #[AsCommand] attribute is resolved from the container when Artisan starts. With the attribute, it's registered by name and built only when you call it. php artisan list and a bare php artisan are the exceptions: both build it too. Or you ask for the service in handle(), and the container resolves it on run. The make:command stub has no constructor, only an empty handle(), so Laravel's default already avoids the trap.
Since Laravel 13, make:command writes a #[Signature] attribute instead of a $signature property. That attribute alone doesn't make the command lazy. Artisan only checks for #[AsCommand]. That's why the example has both.
#[Signature('acme:export-orders')]
#[AsCommand(name: 'acme:export-orders')]
class ExportOrders extends Command
{
public function handle(OrderExporter $exporter): int
{
$this->info($exporter->export() . ' orders exported');
return self::SUCCESS;
}
}
The rule
Keep command constructors cheap, or make them lazy. It holds in both frameworks because it's the same component.
This is the first of 15 short comparisons between Magento 2 and Laravel, one idea at a time, with code. Next: why a save event can fire before the commit in both frameworks, and what that does to a queue.
Which command in your project has the heaviest constructor?
Part 1 of 15 of the Magento 2 vs Laravel series. Originally published on LinkedIn.
Top comments (0)