Symfony Maker helps you create empty commands, controllers, form classes, tests and more so you can forget about writing boilerplate code. This bundle is an alternative to SensioGeneratorBundle for modern Symfony applications and requires using Symfony 3.4 or newer and Symfony Flex.
Run this command to install and enable this bundle in your application:
$ composer require maker
This bundle provides several commands under the make:
namespace. List them
all executing this command:
$ php bin/console list make
make:command Creates a new console command class
make:controller Creates a new controller class
make:entity Creates a new Doctrine entity class
[...]
make:validator Creates a new validator and constraint class
make:voter Creates a new security voter class
The names of the commands are self-explanatory, but some of them include
optional arguments and options. Check them out with the --help
option:
$ php bin/console make:controller --help
In case your applications need to generate custom boilerplate code, you can
create your own make:...
command reusing the tools provided by this bundle.
Imagine that you need to create a make:report
command. First, create a
class that implements :class:`Symfony\\Bundle\\MakerBundle\\MakerInterface`:
// src/Maker/ReportMaker.php namespace AppMaker;
use SymfonyBundleMakerBundleConsoleStyle; use SymfonyBundleMakerBundleDependencyBuilder; use SymfonyBundleMakerBundleInputConfiguration; use SymfonyBundleMakerBundleMakerInterface; use SymfonyBundleMakerBundleStr; use SymfonyBundleMakerBundleValidator; use SymfonyComponentConsoleCommandCommand; use SymfonyComponentConsoleInputInputArgument; use SymfonyComponentConsoleInputInputInterface;
class ReportMaker implements MakerInterface {
public static function getCommandName(): string {
return 'make:report';}
public function configureCommand(Command $command, InputConfiguration $inputConf): void {
- $command
- ->setDescription('Creates a new console command class') ->addArgument('name', InputArgument::OPTIONAL, sprintf('Choose a command name (e.g. <fg=yellow>app:%s</>)', Str::asCommand(Str::getRandomTerm())))
;
}
public function interact(InputInterface $input, ConsoleStyle $io, Command $command): void { }
public function getParameters(InputInterface $input): array {
return array();}
public function getFiles(array $params): array {
return array();}
public function writeNextStepsMessage(array $params, ConsoleStyle $io): void { }
public function configureDependencies(DependencyBuilder $dependencies): void { }
}
For examples of how to complete this class, see the core maker commands.
Make sure your class is registered as a service and tagged with maker.command
.
If you're using the standard Symfony services.yaml
configuration, this
will be done automatically.