Symfony php app console

The Console Component

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

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

Installation

$ composer require symfony/console

If you install this component outside of a Symfony application, you must require the vendor/autoload.php file in your code to enable the class autoloading mechanism provided by Composer. Read this article for more details.

Creating a Console Application

This article explains how to use the Console features as an independent component in any PHP application. Read the Console Commands article to learn about how to use it in Symfony applications.

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

1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/env php  // application.php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Console\Application; $application = new Application(); // . register commands $application->run();

Then, you can register the commands using add():

// . $application->add(new GenerateAdminCommand());

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

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

This is useful when creating a single-command application.

See the Console Commands article for information about how to create commands.

Learn more

  • Console Commands
  • Changing the Default Command
  • Understanding how Console Arguments and Options Are Handled
  • Using Events
  • Cursor Helper
  • Debug Formatter Helper
  • Formatter Helper
  • The Console Helpers
  • Process Helper
  • Progress Bar
  • Progress Indicator
  • Question Helper
  • Table
  • Using the Logger
  • Building a single Command Application
  • Using Console Commands, Shortcuts and Built-in Commands
  • How to Call Other Commands
  • How to Color and Style the Console Output
  • How to Call a Command from a Controller
  • How to Define Commands as Services
  • How to Hide Console Commands
  • Console Input (Arguments & Options)
  • How to Make Commands Lazily Loaded
  • Prevent Running the Same Console Command Multiple Times
  • How to Style a Console Command
  • Verbosity Levels

Источник

Компонент Консоль

Компонент консоль облегчает создание прекрасных и тестируемых интерфейсов командной строки.

Компонент Консоль позволяет вам создавать команды строки. Ваши консольные команды могут быть использованы для любого повторяющегося задания, например, cronjobs, импорта, или других фоновых заданий.

Установка

$ composer require symfony/console

Также вы можете клонировать репозиторий https://github.com/symfony/console.

Если вы устанавливаете этот компонент вне приложения Symfony, вам нужно подключить файл vendor/autoload.php в вашем коде для включения механизма автозагрузки классов, предоставляемых Composer. Детальнее читайте в этой статье.

Создание консольного приложения

Для начала, вам нужно создать PHP-скрипт, чтобы определить консольное приложение:

1 2 3 4 5 6 7 8 9 10 11 12 13
#!/usr/bin/env php  // application.php require __DIR__.'/vendor/autoload.php'; use Symfony\Component\Console\Application; $application = new Application(); // . зарегистрируйте команды $application->run();

Далее, вы можете зарегистрировать команды, используя add():

// . $application->add(new GenerateAdminCommand());

См. статью Команды консоли, чтобы узнать о том, как создавать команды.

Узнайте больше

  • Команды консоли
  • Изменение команды по умолчанию
  • Понимание работы с аргументами и опциями консоли
  • Использование событий
  • Помощник Cursor
  • Помощник Debug Formatter
  • Помощник Formatter
  • Помощники Console
  • Помощник Process
  • Индикатор выполнения
  • Помощник Question
  • Таблица
  • Использование логгера
  • Создание приложения одной команды
  • Использование конспольных команд, ярлыков и встроенных команд
  • Помощники Console
  • Как вызывать другие команды
  • Как раскрашивать и стилизовать вывод консоли
  • Как вызвать команду из контроллера
  • Как определять команды, как сервисы
  • Как скрывать консольные команды
  • Ввод консоли (аргументы и опции)
  • Как сделать команды ленивой загрузки
  • Предотвращение многократного выполения консольной команды
  • Как генерировать URL из консоли
  • Как оформить консольную команду
  • Уровни детализации

Symfony is a trademark of Symfony SAS. Переклад — Playtini. UA RU RU EN

Источник

Читайте также:  Php my admin installer
Оцените статью