Yii Framework – Generate a Controller using yiic shell

Basic tutorial of Yii framework – Creating Controller

We are going to use the yiic shell command to generate a controller of our application. To start the shell, navigate to the root of your learn-yii application folder by running the following command:

>> cd /var/www/learn-yii

Then execute yiic with the following shell command

>> /var/www/yii/framework/yiic shell

yiic shell to create controller

From yiic shell help, you will find out, how to generate controller. the controller command will generate the controller, actions, and the views associated with the specified actions. In this case we want to display message “I’m Learning Yii Framework” using controller. let’s call our controller “message” and let’s name our action method “view” :

>> controller message view

generate controller using yiic shell

MessageController.php already made​​, then go through a text editor and adding the following code in the action method ‘index’:

echo "I’m Learning Yii Framework";

open up your browser and typing this url : http://localhost/learn-yii/index.php?r=message

Ok, next we’ll try to display a text in the view section. Previously we already successfully generate view.php in the /var/www/learn-yii/protected/views/message/ folder. So let’s open up view.php and adding the following HTML code.

Hello, I’m in the view section and I’m still Learning Yii Framework

ok. open up your browser and typing this url to view the result : http://localhost/learn-yii/index.php?r=message/view

message/view - Yii Framework basic tutorial

perfect, the text successfully displayed on the view section

Display an array of data

let’s continue to display an array of data in the view section. Edit the controller by adding a line of code as below:

		echo "I'm Learning Yii Framework";

	public function actionView()

	public function actionViewData(){

Then, let’s make new file in the folder “view/message” and named it “view_data.php” and type the following code:

Display an array of data"; 
foreach($data as $data){
    echo $data."
"; } ?>

Open up your browser and typing this url : http://localhost/learn-yii/index.php?r=message/viewData

Yii Framework - Display an array of data

Creating hyperlink – Getting a little help from Yii CHtml

Yii comes with myriad helper methods that can be used in view templates. These methods exist in the static HTML helper framework class, CHtml. In this case, we want to employ the helper method link which takes in a controllerID/actionID pair, and creates the appropriate hyperlink for you based on how the URL structure is configured for the application. As all these helper methods are static, we can call them directly without the need to create an explicit instance of the CHtml class. Please edit your view.php file and adding the following code as a look like this:


id . '/' . $this->action->id; ?>

Hello, I'm in the view section and I’m still Learning Yii Framework

The code above will take you to the view data array section when you clicked the link. open up your browser and type this url : http://localhost/learn-yii/index.php?r=message/view

Creating hyperlink - Yii Frmaework