Introducing Gii of Yii framework

Introducing Gii of Yii framework

By this time you may learn to install Yii framework in your workstation, If not You can follow this. Today, I will show, how to use GiiModule.

What is Gii?
Gii is a Yii module, which provides web based code generation facilities. Through Gii, we can create all the required files for our MVC model. To work with GiiModule we have to configure it in our application. By default it can be accessible by localhost and we can access the module through our browser. I will show you in detail. Let’s start now.

Step 1: Preparation
Download Yii framework form it’s website. Usually you will get it as an archive format. Extract it and rename this folder as yiiroot. Copy and paste it into your htdocs folder. It will look like as follows –
1 yiiroot

Step 2: Project Creation
We will run yiic application to create our project. You will find it inside yiiroot>framework folder. To run yiic, open your command prompt and go to the htdocs folder. Run the following command.

$ yiic webapp demogii

It will ask your permission to generate required code. This command will create a demogii folder inside your htdocs.

Demogii project

The protected folder, inside demogii contains module, view and controller folders. The protected folder also contains the config folder, which has configuration file for GiiModule. Now, open your browser and write on your address bar http://localhost:8080/demogii/. If everything is ok, you will see demogii application welcome page.

Note: I installed my web server to use 8080 port. If you install it to use port 80 (which is default), you may change the address to http://localhost:80/demogii/ or http://localhost/demogii/

Browsing localhost

Step 3: Enable GiiModule
Now open main.php file with your text editor, from htdocs>demogii>protected>config folder. Uncomment the following code segment to enable GiiModule.

	'password'=>'Enter Your Password Here',
	// If removed, Gii defaults to localhost only. Edit carefully to taste.

Here gii is an associative array. It contains class, password and ipFilters details. For, our purpose, set the value for password and save it. For convenient, I set my password to Password. It will look like this –

Edit main.php

Now go your web browser and write –


It will prompt a password field to login. Give the password what you gave in main.php. In my case password is Password.

Browsing gii from localhost

Congratulation, you are ready use GiiModule.

Yii Code generator

Step 4: Using GiiModule
Now let’s use this mighty code generator to learn about the power of Gii. We will create a controller for demonstration. Follow the sequence –

  1. Click on the Controller Generator.
    Controller Generator of Yii
  2. This will open the Controller Generator page.
    HulloControllerWe need to set the Controller ID. Let’s set –
    Controller ID: HulloNow click, Preview.
  3. It will preview two files. One is for controller and one is for our view.
    HulloController previewNow click on Generate button.
  4. It will physically create HulloController.php for our controller and index.php for our view.
    HulloController generates
  5. Now you can click on the try it now link to view your controller.
    HulloController hulloIndex
  6. If you open htdocs>demogii>protected>views>hullo and htdocs>demogii>protected>controllers folder, you will see index.php for view and HulloController.php for controller, have created.
    HulloController and view

Step 5: Writing Some Code
We already have done with GiiModule. There are also have more advance options available. I will discuss those in future. Now, lets test some code to what we made by Gii. Follow the sequence-

  1. Open htdocs>demogii>protected>controllers>HulloContorller.php with your editor and write the following code.
    class HulloController extends Controller
    	public $message = "Testing Gii in Action!!!";
    	public function actionIndex()
    		$this->render('index', array("content" => $this->message));

    Here, I have initialize one message with $message variable name and trying to send it to our view. Before sending it to view, I put it to an associative array with a key name called content.

  2. Now open htdocs>demogii>protected>views>hullo>index.php with your editor. And write the following code.
    /* @var $this HulloController */
    	<h2>Message from controller: <strong><?php echo $content; ?></strong></h2>
  3. Open your browser and refresh it. You will see the following
    Hulloview index

Step 6: Simpler URL
We are almost done for today. As you can remember, we have a quite complex URL. But it has certain meaning. If you write the URL like –


You will get the same output as before. Confused?

Simpler URL

  1. r : is the route for our application
  2. Hullo: is the controller id
  3. index: is the action id

I think, you are quite clear now. But it is not search engine friendly. We can make it a friendly one. Piece a cake.

Open htdocs>demogii>protected>config>main.php with your editor. Uncomment the following section of code –
Search engine friendly URL

Now open your browser and write the following URL –

Friendly URL on browser
I think, you are happier then before. In this URL you no need to worry about route, we only need controller id and action id.

Same way you can run the GiiModule. Like –


We have just scratched on the surface of the GiiModule. It has lot more feature to discuss, which can help us by generating default code and make our application development process faster.

Happy coding. 🙂

3 comments to “Introducing Gii of Yii framework”

Leave a Reply

Your email address will not be published. Required fields are marked *