In this guide, I will show you how you can create a CRUD application using the yii framework. By using a tool called gii.
Gii is a built in tool for the yii framework which is used to generate models, views and controllers or the MVC. According to Wikipedia, MVC is a software’s architecture, currently considered an architectural pattern used in software engineering. Just read the Wikipedia article for more information about the MVC architecture.
By default the Gii tool is disabled, so the first thing that we need to do is to enable it.
If you’re not yet familiar with Yii and this is your first time, then you might want to check the guide that I made on how to install a web app using the yiic command line tool.
After you have followed the guide above. Launch wampserver and navigate to the www directory
Open up the webapp folder or whatever name you gave to the folder where you installed the yii webapp from the first tutorial.
webappFolder > protected > config
The full path would look like this, if you haven’t customized your wampserver installation:
From that directory open up the main.php file
Then copy the text below and paste them to the search box of your text editor. Just don’t use notepad because it sucks.
// uncomment the following to use a MySQL database
Search that one then you’ll be able to find some text below it which are enclosed with the php comment tags.
Just uncomment them by removing the comment tags. This is what the text looks like after uncomment.
'db'=>array( 'connectionString' => 'mysql:host=localhost;dbname=cdvd', 'emulatePrepare' => true, 'username' => 'root', 'password' => '', 'charset' => 'utf8', ),
The code above is for mysql database only. If you have a different database then search you corresponding database using the search functionality of your text editor. If it didn’t find it then its not supported.
The code above is the definition of your database. Most of the time you’ll only need to change the ‘dbname’. Since the host is by default localhost. Username is by default root. And password is by default none. Charset is the character set used in your database. By default it is utf8 if you made your database using phpmyadmin.
You can change the character set in mysql console by using the charset command.
Next thing that you’ll need to do is to enable gii tool. Just search ‘gii’ on your text editor. And you’ll find a code which looks like this
'gii'=>array( 'class'=>'system.gii.GiiModule', 'password'=>'12345', ), ),
Just uncomment the code and change your password. Mine is 12345 as you can see in the code above.
Don’t forget to save the changes made to the file.
All you have to do now is to access the gii tool in your browser. Just be sure wampserver is executed and online. You can see that its online if the color of the icon in the tray menu is white. Yellow if it is offline. Just click on restart all services if it is offline. And wait for a few seconds until its colored white.
To access the gii tool, just type in:
On your browser. Of course you will have to change the part which says ‘yiiwebapp’ depending on the name of the folder you named your web application.
Just type in the password that you have set awhile ago. Then click on enter.
Then generate a model. The model class would be automatically generated while you type the name of the table from the database you have defined a while ago.
Click on preview then click on generate to generate the php file that will be saved on the models folder.
If you will check inside this directory, you will find that the model class name is the name which is given to the new php file which is being genrated by the gii tool.
Next, select the crud generator. Then type in the model class which is generated a while ago. The controller ID will also be automatically generated as you type in the model class. Be sure to leave things like this if you don’t want to mess it up. But if you want to experiment, then its ok.
As usual, you need to click on preview then generate.
That’s it, if the generation is successful then you can already access your newly made crud application from the browser by typing in:
Of course, you need to tweak the address a bit to match your own:
- yiiwebapp – folder name of the yii web application in the www directory
- dvd- controller id generated from the model class. Usually it’s the name of the table from your database.
That’s it for this guide, I hope you learned something. And if ever this guide helped you then I’d appreciate this if you shared this site to other people that you know who share the same interest. You can share it on your youtube, facebook or stumbleupon account. Thanks.