<ruby id="bdb3f"></ruby>

    <p id="bdb3f"><cite id="bdb3f"></cite></p>

      <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
        <p id="bdb3f"><cite id="bdb3f"></cite></p>

          <pre id="bdb3f"></pre>
          <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

          <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
          <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

          <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                <ruby id="bdb3f"></ruby>

                企業??AI智能體構建引擎,智能編排和調試,一鍵部署,支持知識庫和私有化部署方案 廣告
                # Symfony 控制臺命令教程 > 原文: [http://zetcode.com/symfony/commands/](http://zetcode.com/symfony/commands/) Symfony 控制臺命令教程介紹了如何在 Symfony 中創建控制臺命令。 我們將在控制臺應用中創建幾個命令。 ## Symfony Symfony 是一組可重用的 PHP 組件和一個用于 Web 項目的 PHP 框架。 Symfony 于 2005 年發布為免費軟件。Symfony 的原始作者是 Fabien Potencier。 Symfony 受到 Spring 框架的極大啟發。 ## Symfony 控制臺組件 Symfony 控制臺組件使我們可以創建命令行命令。 控制臺命令可用于創建 CronJob,導入,批處理作業或某些支持性任務。 Symfony 控制臺命令可以在 Symfony 控制臺應用或 Web 應用中使用。 在本教程中,我們將為控制臺應用創建命令。 ## Symfony 控制臺命令示例 在以下示例中,我們使用 Symfony 控制臺組件創建 Symfony 控制臺應用。 ```php $ mkdir commands $ cd commands ``` 我們創建一個項目目錄并找到它。 ```php $ composer require symfony/console ``` 我們安裝`console`包。 `composer.json` ```php { "name": "Symfony command application", "description": "This application demonstrates the usage of a Symfony command in a console application", "require": { "symfony/console": "^4.2" }, "autoload": { "psr-4": { "App\\": "src" } } } ``` 我們更新`composer.json`文件。 我們啟用`App`名稱空間下`src`目錄中的 PHP 類的自動加載。 ```php $ composer dump-autoload -o ``` 創建文件后,我們需要調用`composer dump-autoload -o`命令,該命令將創建一個將類映射到 PHP 文件的文件。 在應用中,我們將有五個命令: * `TimeCommand` - 顯示當前日期和時間 * `MessageCommand` - 顯示來自用戶輸入的消息 * `ColorCommand` - 以彩色顯示消息 * `BooksCommand` - 在表格中顯示書籍列表 * `AskNameCommand` - 交互式詢問用戶名 這些命令在`src/Command`目錄中創建。 社區必須擴展`Symfony\Component\Console\Command`并實現其`configure()`和`execute()`方法。 之后,將命令與`add()`一起添加到`Symfony\Component\Console\Application`。 `src/Command/TimeCommand.php` ```php <?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class TimeCommand extends Command { protected function configure() { $this->setName('time') ->setDescription('Shows current date and time') ->setHelp('This command prints the current date and time'); } protected function execute(InputInterface $input, OutputInterface $output) { $now = date('c'); $message = sprintf("Current date and time: %s", $now); $output->writeln($message); } } ``` `TimeCommand`顯示當前日期和時間。 ```php protected function configure() { $this->setName('time') ->setDescription('Shows current date and time') ->setHelp('This command prints the current date and time'); } ``` 在`configure()`中,我們使用`setName()`設置命令的名稱。 名稱將顯示在可用命令列表中。 我們還為命令添加了描述和幫助。 ```php protected function execute(InputInterface $input, OutputInterface $output) { $now = date('c'); $message = sprintf("Current date and time: %s", $now); $output->writeln($message); } ``` `InputInterface`用于從用戶獲取輸入,`OutputInterface`用于顯示輸出。 在我們的例子中,我們使用標準 ISO 格式的`date()`獲取當前日期和時間,并使用`writeln()`將其輸出到控制臺。 `src/Command/MessageCommand.php` ```php <?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Input\InputArgument; class MessageCommand extends Command { protected function configure() { $this->setName('msg') ->setDescription('Prints a user provided message') ->setHelp('This command prints a message provided by the user') ->addArgument('msg', InputArgument::REQUIRED, 'Pass a message'); } protected function execute(InputInterface $input, OutputInterface $output) { $message = sprintf('The message is: %s', $input->getArgument('msg')); $output->writeln($message); } } ``` `MessageCommand`打印從用戶的參數檢索到的消息,并將其輸出到控制臺。 ```php $this->setName('msg') ->setDescription('Prints a user provided message') ->setHelp('This command prints a message provided by the user') ->addArgument('msg', InputArgument::REQUIRED, 'Pass a message'); ``` 該參數可以是必需的,也可以是可選的。 `InputArgument::REQUIRED`值使該參數成為必需參數。 ```php $message = sprintf('The message is: %s', $input->getArgument('msg')); $output->writeln($message); ``` 我們從輸入中檢索帶有`getArgument()`的參數,然后使用`writeln()`將該參數寫入控制臺。 `src/Command/ColorCommand.php` ```php <?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; use Symfony\Component\Console\Formatter\OutputFormatterStyle; class ColorCommand extends Command { protected function configure() { $this->setName('colc') ->setDescription('Shows output in color') ->setHelp('This command shows output in color'); } protected function execute(InputInterface $input, OutputInterface $output) { $output->writeln("<info>Today is a windy day</info>"); $outputStyle = new OutputFormatterStyle('red'); $output->getFormatter()->setStyle('redt', $outputStyle); $output->writeln('<redt>Tomorrow will be snowing</redt>'); } } ``` `ColorCommand`以彩色輸出文本。 ```php $output->writeln("<info>Today is a windy day</info>"); ``` 在這種情況下,我們使用內置的`info`格式樣式。 ```php $outputStyle = new OutputFormatterStyle('red'); $output->getFormatter()->setStyle('redt', $outputStyle); $output->writeln('<redt>Tomorrow will be snowing</redt>'); ``` 我們還可以使用`OutputFormatterStyle`創建自定義輸出樣式。 我們的`redt`以紅色顯示文字。 `src/Command/BooksCommand.php` ```php <?php namespace App\Command; use Symfony\Component\Console\Helper\Table; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class BooksCommand extends Command { protected function configure() { $this->setName('books') ->setDescription('Shows books in a table') ->setHelp('This command demonstrates the usage of a table helper'); } protected function execute(InputInterface $input, OutputInterface $output) { $table = new Table($output); $table->setHeaderTitle('Books') ->setHeaders(['Title', 'ISBN', 'Author', 'Publisher']) ->setRows([ ['Java Language Features', '978-1-4842-3347-4', 'Kishori Sharan', 'Apress' ], ['Python Testing with pytest', '978-1-68-050-240-4', 'Brian Okken', 'The Pragmatic Programmers' ], ['Deep Learning with Python', '978-1-61729-443-3', 'Francois Chollet', 'Manning' ], ['Laravel up & Running', '978-1-491-93698-5', 'Matt Stauffer', 'O\'Reilly' ], ['Sams Teach Yourself TCP/IP', '978-0-672-33789-5', 'Joe Casad', 'SAMS' ] ]); $table->render(); } } ``` `BooksCommand`使用表格助手以表格格式輸出數據。 ```php $table = new Table($output); ``` 我們創建一個`Table`幫助器的實例。 ```php $table->setHeaderTitle('Books') ->setHeaders(['Title', 'ISBN', 'Author', 'Publisher']) ->setRows([ ['Java Language Features', '978-1-4842-3347-4', 'Kishori Sharan', 'Apress' ], ['Python Testing with pytest', '978-1-68-050-240-4', 'Brian Okken', 'The Pragmatic Programmers' ], ['Deep Learning with Python', '978-1-61729-443-3', 'Francois Chollet', 'Manning' ], ['Laravel up & Running', '978-1-491-93698-5', 'Matt Stauffer', 'O\'Reilly' ], ['Sams Teach Yourself TCP/IP', '978-0-672-33789-5', 'Joe Casad', 'SAMS' ] ]); ``` 我們建立表。 表標題標題由`setHeaderTitle()`指定。 `header`名稱由`setHeaders()`指定。 最后,將數據與`setRows()`相加。 ```php $table->render(); ``` 該表使用`render()`呈現。 `src/Command/AskNameCommand.php` ```php <?php namespace App\Command; use Symfony\Component\Console\Command\Command; use Symfony\Component\Console\Question\Question; use Symfony\Component\Console\Input\InputInterface; use Symfony\Component\Console\Output\OutputInterface; class AskNameCommand extends Command { protected function configure() { $this->setName('ask') ->setDescription('Interactively asks name from the user') ->setHelp('This command asks a user name interactively and prints it'); } protected function execute(InputInterface $input, OutputInterface $output) { $helper = $this->getHelper('question'); $question = new Question("Enter your name: ", "guest"); $name = $helper->ask($input, $output, $question); $message = sprintf("Hello %s!", $name); $output->writeln($message); } } ``` `AskNameCommand`使用問題助手來請求用戶輸入。 ```php $helper = $this->getHelper('question'); ``` 使用`getHelper()`創建一個問題幫助器。 ```php $question = new Question("Enter your name: ", "guest"); ``` 創建一個新的`Question`問題。 第二個參數是默認值。 ```php $name = $helper->ask($input, $output, $question); ``` 問題通過`ask()`激活。 用戶輸入存儲在`$name`變量中。 ```php $message = sprintf("Hello %s!", $name); ``` 我們使用`sprintf()`從用戶輸入構建消息。 ```php $output->writeln($message); ``` 最后,該消息在終端中顯示為`writeln()`。 使用`Symfony\Component\Console\Application`創建一個新的 Symfony 應用。 `Application.php` ```php <?php require __DIR__ . '/vendor/autoload.php'; use App\Command\TimeCommand; use App\Command\BooksCommand; use App\Command\ColorCommand; use App\Command\AskNameCommand; use App\Command\MessageCommand; use Symfony\Component\Console\Application; $app = new Application(); $app->add(new MessageCommand()); $app->add(new TimeCommand()); $app->add(new AskNameCommand()); $app->add(new BooksCommand()); $app->add(new ColorCommand()); $app->run(); ``` 我們用五個命令創建一個 Symfony 控制臺應用。 ```php $app = new Application(); ``` 創建一個新的控制臺應用。 ```php $app->add(new MessageCommand()); $app->add(new TimeCommand()); $app->add(new AskNameCommand()); $app->add(new BooksCommand()); $app->add(new ColorCommand()); ``` 我們向應用添加命令。 ```php $app->run(); ``` 應用從`run()`啟動。 ```php $ php application.php list Console Tool ... Available commands: ask Interactively asks name from the user books Shows books in a table colc Shows output in color help Displays help for a command list Lists commands msg Prints a user provided message time Shows current date and time ``` 我們可以獲得命令列表。 ```php $ php application.php books +----------------------------+--------------- Books -----------------+---------------------------+ | Title | ISBN | Author | Publisher | +----------------------------+--------------------+------------------+---------------------------+ | Java Language Features | 978-1-4842-3347-4 | Kishori Sharan | Apress | | Python Testing with pytest | 978-1-68-050-240-4 | Brian Okken | The Pragmatic Programmers | | Deep Learning with Python | 978-1-61729-443-3 | Francois Chollet | Manning | | Laravel up & Running | 978-1-491-93698-5 | Matt Stauffer | O'Reilly | | Sams Teach Yourself TCP/IP | 978-0-672-33789-5 | Joe Casad | SAMS | +----------------------------+--------------------+------------------+---------------------------+ ``` 我們運行`books`命令。 ```php $ php application.php time Current date and time: 2018-12-20T23:27:16+01:00 ``` 我們運行`time`命令。 在本教程中,我們在 Symfony 控制臺應用中創建了五個控制臺命令。 ```php $ php application.php ask Enter your name: Peter Hello Peter! ``` 我們運行`ask`命令。 在本教程中,我們在 Symfony 控制臺應用中創建了五個控制臺命令。 您可能也對以下相關教程感興趣: [Symfony 簡介](/symfony/intro/), [Symfony 驗證教程](/symfony/validation/), [Symfony Flash 消息](/symfony/flash/), [Symfony 服務教程](/symfony/service/) , [Symfony 表單教程](/symfony/form/), [PHP 教程](/lang/php/)。
                  <ruby id="bdb3f"></ruby>

                  <p id="bdb3f"><cite id="bdb3f"></cite></p>

                    <p id="bdb3f"><cite id="bdb3f"><th id="bdb3f"></th></cite></p><p id="bdb3f"></p>
                      <p id="bdb3f"><cite id="bdb3f"></cite></p>

                        <pre id="bdb3f"></pre>
                        <pre id="bdb3f"><del id="bdb3f"><thead id="bdb3f"></thead></del></pre>

                        <ruby id="bdb3f"><mark id="bdb3f"></mark></ruby><ruby id="bdb3f"></ruby>
                        <pre id="bdb3f"><pre id="bdb3f"><mark id="bdb3f"></mark></pre></pre><output id="bdb3f"></output><p id="bdb3f"></p><p id="bdb3f"></p>

                        <pre id="bdb3f"><del id="bdb3f"><progress id="bdb3f"></progress></del></pre>

                              <ruby id="bdb3f"></ruby>

                              哎呀哎呀视频在线观看