Command Line (Console)
Symfony cung cấp hệ thống dòng lệnh rất mạnh mẽ thông qua Symfony Console Component, cho phép bạn chạy các tác vụ tự động như: nhập dữ liệu, xử lý batch, gửi email, dọn dẹp cache… Trong bài học này, bạn sẽ học cách:
- Sử dụng các lệnh có sẵn của Symfony.
- Tạo lệnh console tùy chỉnh.
- Thực thi và xử lý đầu vào/đầu ra từ dòng lệnh.

1. Giới thiệu Console Component
Symfony Console cho phép xây dựng các lệnh chạy qua CLI. Mỗi lệnh là một class kế thừa từ Command
.
Câu lệnh mẫu:
php bin/console app:say-hello
Các lệnh mặc định của Symfony có thể liệt kê bằng:
php bin/console list
2. Cấu trúc tạo command
Lệnh console được đặt trong thư mục:
src/Command/
Tạo command mới:
php bin/console make:command App\Command\HelloCommand
Hoặc thủ công:
// src/Command/HelloCommand.php
namespace App\Command;
use Symfony\Component\Console\Command\Command;
use Symfony\Component\Console\Input\InputInterface;
use Symfony\Component\Console\Output\OutputInterface;
class HelloCommand extends Command
{
protected static $defaultName = 'app:say-hello';
protected function configure()
{
$this->setDescription('In ra lời chào');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$output->writeln('Xin chào từ Symfony!');
return Command::SUCCESS;
}
}
Chạy lệnh:
php bin/console app:say-hello
3. Nhận tham số và tùy chọn
a. Tham số bắt buộc
use Symfony\Component\Console\Input\InputArgument;
protected function configure()
{
$this
->setDescription('Chào một người cụ thể')
->addArgument('name', InputArgument::REQUIRED, 'Tên người cần chào');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$name = $input->getArgument('name');
$output->writeln("Chào bạn, $name!");
return Command::SUCCESS;
}
Chạy lệnh:
php bin/console app:say-hello Alice
b. Tùy chọn (option)
use Symfony\Component\Console\Input\InputOption;
protected function configure()
{
$this
->addOption('uppercase', null, InputOption::VALUE_NONE, 'In hoa lời chào');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$message = 'Chào bạn!';
if ($input->getOption('uppercase')) {
$message = strtoupper($message);
}
$output->writeln($message);
return Command::SUCCESS;
}
Chạy lệnh:
php bin/console app:say-hello --uppercase
4. Ví dụ thực tế: Import dữ liệu
Giả sử bạn muốn import bài viết từ file CSV vào database.
// src/Command/ImportArticleCommand.php
use App\Entity\Article;
use Doctrine\ORM\EntityManagerInterface;
class ImportArticleCommand extends Command
{
protected static $defaultName = 'app:import-articles';
public function __construct(private EntityManagerInterface $em) {
parent::__construct();
}
protected function configure()
{
$this->setDescription('Nhập bài viết từ CSV');
}
protected function execute(InputInterface $input, OutputInterface $output): int
{
$handle = fopen('data/articles.csv', 'r');
while (($data = fgetcsv($handle)) !== false) {
$article = new Article();
$article->setTitle($data[0]);
$article->setContent($data[1]);
$this->em->persist($article);
}
$this->em->flush();
$output->writeln('Import thành công!');
return Command::SUCCESS;
}
}
Chạy:
php bin/console app:import-articles
Kết luận
Trong bài học này, bạn đã nắm được cách:
- Sử dụng các lệnh dòng lệnh của Symfony.
- Tạo và cấu hình các lệnh console tùy chỉnh.
- Nhận tham số và option để tạo lệnh linh hoạt.
- Xây dựng command thực tế để xử lý import dữ liệu.
Console là công cụ không thể thiếu để tự động hóa quy trình và hỗ trợ quản trị ứng dụng hiệu quả. Trong bài tiếp theo, bạn sẽ học về triển khai thực tế với Docker hoặc hosting, một bước quan trọng đưa ứng dụng của bạn từ môi trường local lên môi trường thật.

Với hơn 10 năm kinh nghiệm lập trình web và từng làm việc với nhiều framework, ngôn ngữ như PHP, JavaScript, React, jQuery, CSS, HTML, CakePHP, Laravel..., tôi hy vọng những kiến thức được chia sẻ tại đây sẽ hữu ích và thiết thực cho các bạn.
Xem thêm

Chào, tôi là Vũ. Đây là blog hướng dẫn lập trình của tôi.
Liên hệ công việc qua email dưới đây.
lhvuctu@gmail.com