In this chapter, we will learn how to write a console application and will discover the main differences between web and console apps.
Then, we will create our first console controller, using a practical example to illustrate how to update a database table.
In the final paragraphs, we will see how to set output colors and text formats and how to implement a complete periodic task, such as sending an e-mail with daily reservations. We will cover the following topics in this chapter:
The console is the third application installed by default with the advanced template.
This app is configured to launch commands through a console access, and it has the same application structure of those already seen in the previous chapters. Therefore, in this section, we require a console access to the host.
Compared to the web and API applications used until now, there are some differences.
The public
properties of a controller, in fact, are visible from the command line as option
. It is required to extend the option()
method of the controller to make those properties available. Also, based on specific action, action parameters are passed as arguments of the command line.
Finally, a console controller action can return an exit code, a number where 0 indicates that everything is OK, a best practice for console application development.
Here is a typical usage of the console application starting from a shell:
yii <route> [--option1=value1 --option2=value2 ... argument1 argument2 ...]
The elements of the preceding code are explained as follows:
route
: This indicates the controller/action
path to be calledoption
: This indicates the accessible public
properties of the controller for that specific action; we can access only the public properties returned by the options()
method of the controllerargument
: This indicates the arguments to be passed to the controller actionYii provides a set of core console applications, which we can access by calling the help
controller (being a web application, the default action will be index
), so as to display everything concerning the list of available console controllers or details about a single controller or action controller.
Let's consider an example; open the command line (in this case, a Linux shell) and type the following from the project root:
$ ./yii help
This will display an output similar to the following (partially displayed):
This is Yii version 2.0.4. 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. … …
Here, the first grouping level represents the controller names (with relative descriptions on the right), and the second level includes the actions of the relative controller. We will require a more deep response when passing the name of controller to help it:
$ ./yii help message
To display the controller description and the list of the actions, we can also require help about the complete route (controller/action) typing:
$ ./yii help message/config
This returns an output containing the description of the action, its usage, and the options available:
DESCRIPTION Creates a configuration file for the "extract" command. The generated configuration file contains detailed instructions on how to customize it to fit for your needs. After customization, you may use this configuration file with the "extract" command. USAGE yii message/config <filePath> [...options...] - filePath (required): string output file name or alias. OPTIONS --appconfig: string custom application configuration file path. If not set, default application configuration is used. --color: boolean, 0 or 1 whether to enable ANSI color in the output. If not set, ANSI color will only be enabled for terminals that support it. --interactive: boolean, 0 or 1 (defaults to 1) whether to run the command interactively.
3.137.164.241