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 names change, but the engineering doesn't. The console is the clearest example.
Same component underneath
In Magento 2, Magento\Framework\Console\Cli, the class behind bin/magento, extends Symfony's Console Application. In Laravel, so does Illuminate\Console\Application, the class behind php artisan. Every command in both frameworks extends Symfony's Command.
So arguments, options, input, output and return codes work the same way. Only registration differs. 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 a fresh Magento 2.4.9 install 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. No Proxy needed.
<!-- 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. Or you ask for the service in handle(), and the container resolves it on run. The make:command stub has no constructor and injects into 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], so 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)