Movatterモバイル変換


[0]ホーム

URL:


Skip to content
Version:

The Console Component

Edit this page

The Console component eases the creation of beautiful and testable commandline interfaces.

The Console component allows you to create command-line commands. Your consolecommands can be used for any recurring task, such as cronjobs, imports, orother batch jobs.

Installation

1
$composer require symfony/console

Note

If you install this component outside of a Symfony application, you mustrequire thevendor/autoload.php file in your code to enable the classautoloading mechanism provided by Composer. Readthis article for more details.

Creating a Console Application

See also

This article explains how to use the Console features as an independentcomponent in any PHP application. Read theConsole Commands article tolearn about how to use it in Symfony applications.

First, you need to create a PHP script to define the console application:

12345678910111213
#!/usr/bin/env php<?php// application.phprequire__DIR__.'/vendor/autoload.php';useSymfony\Component\Console\Application;$application =newApplication();// ... register commands$application->run();

Then, you can register the commands usingadd():

12
// ...$application->add(newGenerateAdminCommand());

You can also register inline commands and define their behavior thanks to theCommand::setCode() method:

12345678
// ...$application->register('generate-admin')    ->addArgument('username', InputArgument::REQUIRED)    ->setCode(function(InputInterface$input, OutputInterface$output):int{// ...        return Command::SUCCESS;    });

This is useful when creating asingle-command application.

See theConsole Commands article for information about how to create commands.

This work, including the code samples, is licensed under aCreative Commons BY-SA 3.0 license.
TOC
    Version

    Symfony 7.3backers


    [8]ページ先頭

    ©2009-2025 Movatter.jp