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 –
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.
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/
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.
'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'Enter Your Password Here', // If removed, Gii defaults to localhost only. Edit carefully to taste. 'ipFilters'=>array('127.0.0.1','::1'), ),
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 –
Now go your web browser and write –
localhost:8080/demogii/index.php?r=gii
It will prompt a password field to login. Give the password what you gave in main.php. In my case password is Password.
Congratulation, you are ready use GiiModule.
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 –
- Click on the Controller Generator.
- This will open the Controller Generator page.
We need to set the Controller ID. Let’s set –
Controller ID: HulloNow click, Preview. - It will preview two files. One is for controller and one is for our view.
Now click on Generate button.
- It will physically create HulloController.php for our controller and index.php for our view.
- Now you can click on the try it now link to view your controller.
- 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.
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-
- 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.
- Now open htdocs>demogii>protected>views>hullo>index.php with your editor. And write the following code.
<?php /* @var $this HulloController */ $this->breadcrumbs=array( 'Hullo', ); ?> <p> <h2>Message from controller: <strong><?php echo $content; ?></strong></h2> </p>
- Open your browser and refresh it. You will see the following
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 –
http://localhost:8080/demogii/index.php?r=Hullo/index
You will get the same output as before. Confused?
- r : is the route for our application
- Hullo: is the controller id
- 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 –
Now open your browser and write the following URL –
http://localhost:8080/demogii/index.php/Hullo/index
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 –
http://localhost:8080/demogii/index.php/gii
Conclusion
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. 🙂
Hej, I’m from Bangladesh. Learning programming is one of the freaking decisions I have taken in my life. Because, it makes me and my life crazy. I have great weakness on open source technologies. Perhaps, that’s why I do not know any closed source language. I fall in love with programming, when I started my undergraduate in East West University. Till now, I can not live without it.
Helpful
Thanks i understood and done my first yii based project thank you
how i migrate table in database