Доступ к консольным программам происходит с помощью скрипта yii
в корне проекта. По умолчанию без параметров он рассказывает о следующих своих возможностях:
asmer@helium ~/public_html/yii/basic $ ./yii
This is Yii version 2.0.10.
The following commands are available:
- asset Allows you to combine and compress your JavaScript and CSS files.
asset/compress (default) Combines and compresses the asset files according to the given
configuration.
asset/template Creates template of configuration file for [[actionCompress]].
- cache Allows you to flush cache.
cache/flush Flushes given cache components.
cache/flush-all Flushes all caches registered in the system.
cache/flush-schema Clears DB schema cache for a given connection component.
cache/index (default) Lists the caches that can be flushed.
- fixture Manages fixture data loading and unloading.
fixture/load (default) Loads the specified fixture data.
fixture/unload Unloads the specified fixtures.
- gii This is the command line version of Gii - a code generator.
gii/controller Controller Generator
gii/crud CRUD Generator
gii/extension Extension Generator
gii/form Form Generator
gii/index (default)
gii/model Model Generator
gii/module Module Generator
- hello This command echoes the first argument that you have entered.
hello/index (default) This command echoes what you have entered as the message.
- help Provides help information about console commands.
help/index (default) Displays available commands or the detailed information
- message Extracts messages to be translated from source files.
message/config Creates a configuration file for the "extract" command using command
line options specified
message/config-template Creates a configuration file template for the "extract" command.
message/extract (default) Extracts messages to be translated from source code.
- migrate Manages application migrations.
migrate/create Creates a new migration.
migrate/down Downgrades the application by reverting old migrations.
migrate/history Displays the migration history.
migrate/mark Modifies the migration history to the specified version.
migrate/new Displays the un-applied new migrations.
migrate/redo Redoes the last few migrations.
migrate/to Upgrades or downgrades till the specified version.
migrate/up (default) Upgrades the application by applying new migrations.
- serve Runs PHP built-in web server
serve/index (default) Runs PHP built-in web server
To see the help of each command, enter:
yii help <command-name>
Структура консольных приложений схожа с обычными контроллерами-экшОнами. Для создания своего контроллера нужно:
commands
\yii\console\Controller
<?php
namespace app\commands;
class ConsoleController extends \yii\console\Controller
{
public function actionIndex() {
echo '<pre>';print_r( func_get_args() );echo '</pre>';
return 0; //Exit Code
}
}
Тестовый контроллер выше работает следующим образом
asmer@helium ~/public_html/yii/basic $ ./yii console/index lalal bbb ccc dddd
<pre>Array
(
[0] => lalal
[1] => bbb
[2] => ccc
[3] => dddd
)
</pre>
В шелле каждая команда возвращает Exit Code, на подобие кода возврата функций в ЯП. Однако 0 обычно означает безошибочное выполнение, другое число - ошибку. Можно сказать что true
и false
работают в шелле наоборот. Этот код передается с помощью
return
в вашем экшОне.