跳转到主内容
极星编程网:以代码为星,赴技术山海!

What Is a Console Application in Yii2?

A Yii2 console application is a CLI-based program that runs PHP scripts outside the web server, enabling background tasks, scheduled jobs, and maintenance operations via commands like php yii . a yii 2 console application is a cli-based program that runs php scripts outside the web server, enabling background tasks, scheduled jobs, and maintenance operations via commands like `php yii `. In Yii2, the console application is a specialized instance of yii\console\Application, which extends yii\base\Application with features tailored for command-line execution. Unlike web applications that respond to HTTP requests, console applications handle input from the terminal and output text-based results—making them ideal for automation, data migration, cron-triggered cleanup, or batch processing. Key Characteristics Runs via PHP CLI (not Apache/Nginx) Bootstrapped using the yii executable script (typically located in your project root) Organized around reusable, self-contained commands , each mapped to a class extending yii\console\Controller Basic Usage Example From your project root, run:
php yii help
This lists all available commands. To execute a built-in command:
php yii migrate/up --interactive=0
This applies pending database migrations non-interactively. WOC-YII开源站群管理系统1.3 WOC-YII是rschome.com基于yii framework 1.1.8框架所开发的一款开源简易站群管理系统。它的功能与WOC完全一样。目前版本为V1.3,新版本正在开发中,同时欢迎大家参与到开发中来! WOC-YII 1.3在1.2的基础上优化了登录系统(密码加密),优化了权限控制系统,新增seo管理功能,新增自动安装向导! 程序框架:yiiframework1.1.8 配置文件:p 下载 Creating a Custom Command Create commands/HelloController.php:
Then run it:
php yii hello/index --name="Yii2" // Output: Hello, Yii2!
Common Use Cases Running database migrations (yii migrate) Generating code scaffolding (yii g/model User) Sending bulk emails or notifications Importing/exporting large datasets Scheduled cleanup (e.g., log rotation via cron) Important Notes ✅ Console apps share the same configuration structure as web apps (e.g., console/config/main.php), but use separate config files and environments. ⚠️ Never expose console scripts via web access—ensure console/ is outside the document root and yii has proper file permissions. ⏱️ For recurring tasks, integrate with system cron or process managers (e.g., * * * * * cd /var/www/myapp && php yii hello/index >> /dev/null 2>&1). By leveraging Yii2’s console application, developers gain a robust, framework-integrated way to manage operational tasks—securely, consistently, and maintainably.

相关文章