How to use the command "php yii" (with examples)
Yii Framework’s command-line interface allows developers to manage their Yii applications through the command line. This article provides examples of three different use cases for the “php yii” command.
Use case 1: Display a list of all available commands
Code:
php yii help
Motivation:
Sometimes developers need to quickly find a list of available commands in Yii in order to understand what functionalities are available to them. By running the “help” command, developers can obtain a comprehensive list of commands, making it easier to explore and utilize various functionalities of the Yii framework.
Explanation:
The “help” command is used to display a list of all available commands in Yii.
Example output:
This is Yii version 3.2.2.
Commands:
console Yii Framework console runner.
help/list Lists available commands.
serve Start PHP's built-in web server for the current Yii application.
gii Yii Code Generator.
Use case 2: Start PHP’s built-in web server for the current Yii application
Code:
php yii serve
Motivation:
During the development process, it is often necessary to test and preview a Yii application locally. The “serve” command allows developers to quickly start PHP’s built-in web server and access their Yii application in a local environment. This is especially useful when working on frontend tasks or validating the behavior of backend APIs.
Explanation:
The “serve” command starts PHP’s built-in web server for the current Yii application.
Example output:
Server started on http://localhost:8080/
Document root is "/path/to/your/yii-application/web"
Quit the server with CTRL-C or COMMAND-C.
Use case 3: Generate a controller, views and related files for the CRUD actions on the specified model class
Code:
php yii gii/crud --modelClass=ModelName --controllerClass=ControllerName
Motivation:
Developers often need to quickly generate a set of CRUD (Create, Read, Update, Delete) actions for a specific model class in their Yii application. The “gii/crud” command allows developers to automate the process of generating the necessary files, saving time and effort when setting up basic CRUD functionalities.
Explanation:
The “gii/crud” command generates a controller, views, and related files for the CRUD actions on the specified model class.
Arguments:
–modelClass: The name of the model class for which the CRUD actions need to be generated.
–controllerClass: The name of the controller class that will handle the CRUD actions.
Example output:
Controller class 'ControllerName' created successfully in '/path/to/your/yii-application/controllers/ControllerName.php'.
Updated file '/path/to/your/yii-application/views/controller-name/index.php'.
...
Conclusion:
The “php yii” command offers a wide range of functionalities for managing Yii applications through the command line. It allows developers to quickly access a list of available commands, start a built-in web server, and generate CRUD actions for specified models. By leveraging the power of the command-line interface, developers can efficiently develop and maintain their Yii applications with ease.