Make your own web application with the Yii Framework: Tutorial

Yii is a quick and secure system for creating PHP applications. It has brilliant support for different databases.

Yii can be utilized for building up any sort of web applications. It is particularly befitted for high traffic web applications like forums, social networking sites, portals etc.

We hear many times about every single diverse framework for PHP. Here, I will endeavor to demonstrate to you the snappiest method to make a Yii web application utilizing the inbuilt tools provided by Yii. This should be possible in only a couple of steps.

So, let’s see how

First, download latest source of Yii source.

Its time now to make our first Yii Application. You can utilize yiic command to do that through the command line and Gii (powerful web-based code generator) to automate code work for some activities.

Extract the downloaded zip file in your project directory. Run yiic on the command line as givenbelow –

% YiiRoot/framework/yiicwebappWebRoot/testdrive

This will form a skeleton Yii application within the directory WebRoot/testdrive. The application has a directory framework that is required by maximum Yii applications.

When you have run that command, you can see this application by going to –

http://hostname/testapp/index.php

TROUBLESHOOTING

In case that while creating a new application through the command line you get the error which reads as:

“‘php.exe’ is not recognized as an internal or external command, operable program or batch file”

Re-check your set up is configured correctly.

So simple right? Just two steps and you as of now have a web skeleton ready with you. Without writing even a single code, we can test drive our first Yii application. Now, move further to interface this application to a database.

probytes

To handle the database, we have to open the file main.php in the directory config. Uncomment db array and enter the appropriate database settings as follows:

 

As we have prepared the database connection, we can begin building multiple pages and modules. How to do that?

Similarly, there is automated tool to do this as well. This is called gii tool. You’ll find there are alternatives to generate several things such as CRUD, Models etc.

Now we’ll begin a fun part. We might want to execute CRUD activities. This is likewise ordinarily required in functional applications by using Gii tool.

To utilize Gii, we first need to modify the file WebRoot/testdrive/protected/config/main.php, which is called the application configuration file:

 

You can visit http://hostname/testapp/index.php?r=gii for getting the access to the code generator. We will be indicated for a password, which ought to be the one that we entered in the above application setup.

Now, we will generate the user model. After login, tap on the Model Generator link. This will take us to the page of model generation.

On the Table Name field, enter tbl_user. In the Model Class field, enter User. At that point click Preview. This will display to us the new code file to be created. Presently click the Generate button. Another file with name User.php will be created beneath protected/models. This User model class enables us to converse with the fundamental database tbl_user table in an object-oriented pattern.

On making the model class file, we will create the code that executes the CRUD tasks about the user information. We pick the Crud Generator in Gii.

In the Model Class field, insert User. In the Controller ID field, enter in lowercase –“user”. Then click the Preview button alongside the Generate button. We have terminated the CRUD code generator.

Now it’s time for accessing the CRUD Pages.

Let’s have some more fun with our work by browsing the given URL –

http://hostname/testdrive/index.php?r=user

This will reveal to us the user entries list in the tbl_user table.

Press the Create User button on the page.

We will be taken to the login page in case that we have not signed in previously. Subsequent to signing in, we will see an input form that enables us to include an entry for a new user. Finish that form and tap on the button- Create. In case that there is any input mistake, an error message will incite which stopsus from saving the information. On the user list page, we should see the recently included user showing up in the rundown.

yii

You can rehash the above steps to enter more users. Notice that the user list page will naturally paginate the entries if there are a lot of entries to be shown on one page.

In case that we log in as an administrator utilizing admin/admin, we can see the user administrator page with the below-given URL –

http://hostname/testdrive/index.php?r=user/admin

This will demonstrate to us the user entries in a decent tabular layout. We can tap on the table header cells to classify the relating segments. We can tap on the buttons on every line of data to view, delete or refreshthe corresponding line of data. We can also browse various pages. Additionally, we can filter and scan to search for the information we want to access.

So, you must have learned by now that all these decent characteristics are so easily accessible without expecting us to write a single line of code. All thanks to the Yii framework! 

Copyright © 2024 Probytes.