যারা বাংলাদেশের ৩৬ তম Zend Certified Engineer এবং এই সাইট (w3programmers) এর স্বত্বাধিকারী এবং লেখক Masud Alam স্যার এর কাছে কাছে সপ্তাহে ২ দিন ক্লাস, ৬ দিন প্রাকটিস করতে চান তাদের জন্য আগামী ১ February 2018 (বৃহস্পতি এবং শুক্রবার সন্ধ্যা ৬:০০ টা রাত ৯:০০ টা) থেকে শুরু হতে যাচ্ছে ৫ মাস ব্যাপী Professional Web Design & Development With HTML, CSS, JAVASCRIPT, JQUERY, PHP & MySQL for Beginners কোর্স (এই কোর্স টি যারা ফ্রিল্যান্সিং এবং আউটসোর্সিং কাজে আগ্রহী বিশেষ ভাবে তাদের জন্য design করা হয়েছে। ), আগামী ৪ February 2018 ( রবি এবং মঙ্গলবার সন্ধ্যা ৬:০০ টা থেকে রাত ৯:০০ টা ) থেকে শুরু হতে যাচ্ছে ৪ মাস ব্যাপী Zend Certified PHP 7.1 Engineering কোর্স। যেটা Advance PHP কোর্স নামে পরিচিত । এবং ৫ February 2018 (সোম এবং বুধবার সন্ধ্যা ৬:০০ টা থেকে রাত ৯:০০ টা) থেকে শুরু হতে যাচ্ছে ৪ মাস ব্যাপী Laravel 5.5 Professional Beginning to Advance Course কোর্স ।এই কোর্স গুলোতে তিনি ছাত্রদের কে সপ্তাহে ২ দিন ৩ ঘন্টা করে ৬ ঘন্টা ক্লাস নিবেন , এবং শনি থেকে বৃহস্পতি সপ্তাহে ৬ দিন ই স্যার এর অফিসে বসে প্রাকটিস করার সুযোগ দিবেন ! এতে প্রাকটিস করা কালীন ছাত্র-ছাত্রীরা কোনো সমস্যায় পড়লে তাৎক্ষণিক স্যার এর সহযোগিতা পাবে . (উল্লেখ্য: Training গুলো স্যার তার techbeeo software company'r পাশাপাশি পরিচালনা করে থাকেন।) কোর্স গুলো সম্পর্কে বিস্তারিত জানতে পারবেন এই লিংকে : Course Details .কোর্স গুলোর প্রত্যেকটির ফী নির্ধারণ করা হয়েছে ৩০,০০০ টাকা। আগ্রহীদেরকে অতিসত্বর মাসুদ আলম স্যার এর সাথে যোগাযোগ করার জন্য অনুরোধ করা যাচ্ছে। স্যার এর মোবাইল নম্বর : ০১৭২২ ৮১ ৭৫ ৯১

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.

'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 –

Edit main.php

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.

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.
    <?php
    /* @var $this HulloController */
    
    $this->breadcrumbs=array(
    	'Hullo',
    );
    ?>
    
    <p>
    	<h2>Message from controller: <strong><?php echo $content; ?></strong></h2>
    </p>
    
    
  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 –

http://localhost:8080/demogii/index.php?r=Hullo/index

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 –

http://localhost:8080/demogii/index.php/Hullo/index
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 –

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.

3 comments on “Introducing Gii of Yii framework

Leave a Reply

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