网页 综合 更新时间 2026-11-13 17:27:18

Console Commands (Symfony Docs)

自动蜘蛛 symfony.com 正文约 6000 字
Console Commands (Symfony Docs)
The Symfony framework provides lots of commands through the bin/console script (e.g. the well-known bin/console cache:clear command). These commands are created with the Console component. You can also use it to create your own commands, both in Symfony applications and in any other PHP project.
Symfony applications include the Console component by default. In other PHP projects, install it with Composer:
Each Symfony application comes with a large set of commands. You can use the list command to view all available commands in the application:
list is the default command, so running php bin/console is the same.
If you find the command you need, you can run it with the --help option to view the command's documentation:
--help is one of the built-in global options from the Console component, which are available for all commands, including those you can create. To learn more about them, you can read this section.
You don't have to type the full command names. You can type the shortest unambiguous name to run a command. When command names use : to define namespaces, you only need to type the shortest unambiguous text for each part:
If the short name is ambiguous (i.e. more than one command matches it), no command is run and Symfony displays the matching commands so you can choose one of them.
Console commands run in the environment defined in the APP_ENV variable of the .env file, which is dev by default. It also reads the APP_DEBUG value to turn "debug" mode on or off (it defaults to 1, which is on).
To run the command in another environment or debug mode, edit the value of APP_ENV and APP_DEBUG. You can also define these env vars when running the command, for instance:
If you are using the Bash, Zsh or Fish shell, you can install Symfony's completion script to get auto completion when typing commands in the terminal. All commands support name and option completion, and some can even complete values.
First, you have to install the completion script once. Run bin/console completion --help for the installation instructions for your shell.
When using Bash, make sure you installed and set up the "bash completion" package for your OS (typically named bash-completion).
After installing and restarting your terminal, you're all set to use completion (by default, by pressing the Tab key).
Many PHP tools are built using the Symfony Console component (e.g. Composer, PHPStan and Behat). If they are using version 5.4 or higher, you can also install their completion script to enable console completion:
If you are using the Symfony CLI tool, follow these instructions to enable autocompletion.
Commands are defined in classes and auto-registered using the #[AsCommand] attribute. For example, you may want a command to create a user:
You can also use #[AsCommand] to add a description, usage examples, and longer help text for the command:
Additionally, you can extend the Command class to leverage advanced features like lifecycle hooks (e.g. initialize() and interact()):
Support for method-based console commands was introduced in Symfony 8.1.
Instead of creating one class per command, you can define multiple commands in the same class by adding the #[AsCommand] attribute to individual public methods. This is useful to group related commands that share the same dependencies:
Each annotated method becomes an independent command that can be run, listed and tested separately.
If you also add the #[AsCommand] attribute to the class, its name becomes the prefix of the command names defined in the methods (in the same way as a #[Route] attribute on a controller class prefixes the routes of its methods). The following class registers the same app:user:create and app:user:delete commands as the previous example:
The prefix is also added to the command aliases (the alias of the second command is app:user:remove). Method-level names must be relative to the prefix: using app:user:create instead of create in the previous example throws an exception.
If the class defines an __invoke() method, Symfony also registers it as the app:user command; otherwise, the class-level attribute only defines the prefix. In addition, if the class-level attribute sets hidden: true, all the method-based commands are hidden too.
When using the Console component without the service container, you can register method-based commands manually:
In Symfony applications, commands are registered automatically. If you're using the default services.yaml configuration, your command classes are already registered as services and Symfony finds them thanks to the #[AsCommand] attribute and autoconfiguration. If you can't use PHP attributes, register the command as a service and tag it with the console.command tag.
Registering console commands by overriding the Bundle::registerCommands() method is deprecated. Use the #[AsCommand] attribute or the console.command service tag instead.
Commands are loaded lazily: their classes are only instantiated when the command is run. This is possible because Symfony reads the command name from the #[AsCommand] attribute without instantiating the class. When registering commands manually with the console.command tag, set the command name in the command attribute of the tag to get the same behavior:
Running the list command instantiates all commands, including lazy ones. The only exception is commands defined as LazyCommand instances, whose command factory is not executed.
In standalone applications, create the console application in a PHP script and register your commands with the addCommand() method:
You can also define commands inline with a closure, without creating a class. Use the register() method and pass the closure to setCode(). The closure accepts the same #[Argument] and #[Option] parameters as the __invoke() method:
After configuring and registering the command, you can run it in the terminal:
As you might expect, this command will do nothing as you didn't write any logic yet. Add your own logic inside the __invo
访问原文 返回搜索 https://symfony.com/doc/current/console.html
本页为82搜索生成的网页快照,内容来自站内索引;演示数据的原文链接仅作示例。