Frogg logo

Frogg's web tools

Hand Crafted Tools

Home > Coding Symfony > Symfony Console
Welcome on Frogg's web tools | Current date :
19/03/2024

Symfony Console

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.

The list of available commands can be display using the command:
php bin/console

Official website : Symfony Console

Usage

command [options] [arguments]

Options

-h, --helpDisplay this help message
-q, --quietDo not output any message
-V, --versionDisplay this application version
--ansiForce ANSI output
--no-ansiDisable ANSI output
-n, --no-interactionDo not ask any interactive question
-e, --env=ENVThe Environment name. [default: "dev"]
--no-debugSwitches off debug mode.
-v|vv|vvv, --verboseIncrease the verbosity of messages: 1 for normal output, 2 for more verbose output and 3 for debug

Commands

aboutDisplays information about the current project
helpDisplays help for a command
listLists commands
assets
assets:installInstalls bundles web assets under a public directory
cache
cache:clearClears the cache
cache:pool:clearClears cache pools
cache:pool:prunePrune cache pools
cache:warmupWarms up an empty cache
config
config:dump-referenceDumps the default configuration for an extension
debug
debug:autowiringLists classes/interfaces you can use for autowiring
debug:configDumps the current configuration for an extension
debug:containerDisplays current services for an application
debug:event-dispatcherDisplays configured listeners for an application
debug:routerDisplays current routes for an application
debug:twigShows a list of twig functions, filters, globals and tests
lint
lint:yamlLints a file and outputs encountered errors
lint:twigLints a template and outputs encountered errors
router
router:matchHelps debug routes by simulating a path info match
doctrine
doctrine:cache:clear-collection-regionClear a second-level cache collection region
doctrine:cache:clear-entity-regionClear a second-level cache entity region
doctrine:cache:clear-metadataClears all metadata cache for an entity manager
doctrine:cache:clear-queryClears all query cache for an entity manager
doctrine:cache:clear-query-regionClear a second-level cache query region
doctrine:cache:clear-resultClears result cache for an entity manager
doctrine:cache:containsCheck if a cache entry exists
doctrine:cache:deleteDelete a cache entry
doctrine:cache:flush[doctrine:cache:clear] Flush a given cache
doctrine:cache:statsGet stats on a given cache provider
doctrine:database:createCreates the configured database
doctrine:database:dropDrops the configured database
doctrine:database:importImport SQL file(s) directly to Database.
doctrine:ensure-production-settingsVerify that Doctrine is properly configured for a production environment
doctrine:generate:entities[generate:doctrine:entities] Generates entity classes and method stubs from your mapping information
doctrine:mapping:convert[orm:convert:mapping] Convert mapping information between supported formats
doctrine:mapping:importImports mapping information from an existing database
doctrine:mapping:info
doctrine:migrations:diffGenerate a migration by comparing your current database to your mapping information.
doctrine:migrations:executeExecute a single migration version up or down manually.
doctrine:migrations:generateGenerate a blank migration class.
doctrine:migrations:latestOutputs the latest version number
doctrine:migrations:migrateExecute a migration to a specified version or the latest available version.
doctrine:migrations:statusView the status of a set of migrations.
doctrine:migrations:versionManually add and delete migration versions from the version table.
doctrine:query:dqlExecutes arbitrary DQL directly from the command line
doctrine:query:sqlExecutes arbitrary SQL directly from the command line.
doctrine:schema:createExecutes (or dumps) the SQL needed to generate the database schema
doctrine:schema:dropExecutes (or dumps) the SQL needed to drop the current database schema
doctrine:schema:updateExecutes (or dumps) the SQL needed to update the database schema to match the current mapping metadata
doctrine:schema:validateValidate the mapping files
make
make:authCreates an empty Guard authenticator
make:commandCreates a new console command class
make:controllerCreates a new controller class
make:entityCreates a new Doctrine entity class
make:fixturesCreates a new class to load Doctrine fixtures
make:formCreates a new form class
make:functional-testCreates a new functional test class
make:migrationCreates a new migration based on database changes.
make:serializer:encoderCreates a new serializer encoder class
make:subscriberCreates a new event subscriber class
make:twig-extensionCreates a new Twig extension class
make:unit-testCreates a new unit test class
make:validatorCreates a new validator and constraint class
make:voterCreates a new security voter class
server
server:logStarts a log server that displays logs in real time
server:runRuns a local web server
server:startStarts a local web server in the background
server:statusOutputs the status of the local web server for the given address
server:stopStops the local web server that was started with the server:start command
  • The list of commands may vary in function of your components installed.

Example

symfony debug routes

Creating a Command

Official website : Symfony Console

Commands are defined in classes extending Command. For example, you may want a command to create a user:

Example of command

// src/Command/CreateUserCommand.php
	namespace App\Command;

	use Symfony\Component\Console\Command\Command;
	use Symfony\Component\Console\Input\InputInterface;
	use Symfony\Component\Console\Output\OutputInterface;

	class CreateUserCommand extends Command
	{
		protected function configure()
		{
		$this
			// the name of the command (the part after "bin/console")
			->setName('app:create-user')

			// the short description shown while running "php bin/console list"
			->setDescription('Creates a new user.')

			// the full command description shown when running the command with
			// the "--help" option
			->setHelp('This command allows you to create a user...')
		;
		}

		protected function execute(InputInterface $input, OutputInterface $output)
		{
			// outputs multiple lines to the console (adding "\n" at the end of each line)
			$output->writeln([
				'User Creator',
				'============',
				'',
			]);

			// outputs a message followed by a "\n"
			$output->writeln('Whoa!');

			// outputs a message without adding a "\n" at the end of the line
			$output->write('You are about to ');
			$output->write('create a user.');
		}
	}

Executing the command

php bin/console app:create-user

will display symfony custom command

Console command updated

As you can see the new commands will apear in App section : symfony custom command

Clear all cache

It can occur that a php error page is due to the cache files.
Before loosing hour in trying to debug a working code, make sure you clear the cache : copy to clipboard
composer dump-autoload
php bin/console cache:clear
Page created by the 28/02/2018 22:57
Generated in 0.001 sec & displayed in ... sec