Introduction to the YII Framework for PHP tutorial _php instance

Source: Internet
Author: User
Tags generator one table php script php tutorial sqlite sqlite database file permissions yii

Installation

The installation of YII consists of the following two steps:

Download the YII framework from yiiframework.com.
Extract the Yii compression pack into a WEB accessible directory.
tip| Hint: Installation in the Web directory is not necessary, each YII application has a portal script, only it must be exposed to the Web user. Other PHP scripts (including Yii) should be protected from WEB access because they could be exploited by hackers.
Demand

After you install Yii you may want to verify that your server meets the requirements of using Yii, simply enter the following URL in the browser to access the requirements detection script:

http://hostname/path/to/yii/requirements/index.php
The minimum requirement for YII is that your WEB server supports PHP 5.1.0 or later. Yii is tested through the Apache HTTP server on Windows and Linux systems and should work properly on other WEB servers and platforms that support PHP 5.

Establish the first YII application

In order to have a preliminary understanding of yii, we describe how to build the first Yii application in this section. We will use the YIIC (command-line tool) to create a new YII application. Gii (a powerful web-based code generator) completes automatic code generation for specific tasks. Assuming Yiiroot is the installation directory for YII, WebRoot is the server's document root directory.

Run YIIC at the command line, as follows:

% YIIROOT/FRAMEWORK/YIIC WebApp webroot/testdrive

note| Note: When running YIIC on MacOS, Linux, or Unix systems, you may need to modify the Yiic file permissions to enable it to run. In addition, you can run this tool like this:

% CD WebRoot
% php yiiroot/framework/yiic.php webapp testdrive

This will create a basic YII application in the Webroot/testdrive directory. This application has the directory structure required for most Yii applications.

Instead of writing a line of code, we can access the following URL in our browser to see our first YII application:

http://hostname/testdrive/index.php
As we can see, this application contains three pages: the first page, the contact page, the login page. Home Show some information about the application and user login status, contact page displays a contact form for users to fill out and submit their inquiries, the login page allows users to pass the authentication and then access the authorized content. See the screenshot below for more information:

The following tree diagram describes the directory structure of our application.

testdrive/index.php WEB Application Portal script file index-test.php the portal script file used by the functional test assets/contains the exposed resource file css/ Contains CSS files images/contains picture files themes/contains application theme protected/contains protected application files YIIC YIIC command Row script yiic.bat Windows YIIC command line script yiic.php YIIC command line PHP script commands/contains custom ' YIIC ' command s hell/contains a custom ' yiic Shell ' command components/a reusable user component controller.php the base class for all controller classes identity.php used to Authenticated ' Identity ' class config/contains configuration files console.php Console application configuration main.php WEB application configuration test.php Functional Testing Use the configuration controllers/contains the controller's class file sitecontroller.php the default controller's class file data/contains the sample database Schema.mysql.sql sample MySQL database Schema.sqlite.sql Sample SQLite database testdrive.db Example SQLite database file extensions/contains Third-party extensions Messag Es/contains the translated message models/the class file that contains the model loginform.php the form model of the ' Login ' action contactform.php ' contact ' action form Model runtime/containsThe temporarily generated file tests/contains the test script views/the view and layout file containing the controller layouts/contains layout view files main.php The default layout for all views  column1.php use a single-column page layout column2.php A layout that uses a two-column page site/A view file containing the ' site ' controller pages/contains
      Static page about.php view of "about" page contact.php view of ' contact ' action error.php view of ' Error ' Action (show external error)
 View of the index.php ' index ' action login.php The view of the ' Login ' action system/contains system view files

Connecting to a database

Most WEB applications are database driven and our test applications are no exception. To use a database, we first need to tell the application how to connect it. Modify the applied configuration file webroot/testdrive/protected/config/main.php, as follows:

Return Array (
 ...
 ') Components ' =>array (
 ...
 ') DB ' =>array (
  ' connectionString ' => ' sqlite:protected/data/source.db ',
 ),
 ),
 ...
);

The above code tells the YII application to connect to the SQLite database webroot/testdrive/protected/data/testdrive.db when needed. Note that this SQLite database is already included in the application framework we created. The database contains only one table named Tbl_user:

CREATE TABLE tbl_user (
  ID INTEGER not NULL PRIMARY KEY AutoIncrement,
  username VARCHAR (128) isn't null,
  pass Word VARCHAR (128) Not NULL,
  e-mail VARCHAR (128) not null
);

If you want to change to a MySQL database, you need to import the file Webroot/testdrive/protected/data/schema.mysql.sql to build the database.

note| Note: To use YII's database capabilities, we need to enable PHP's PDO extensions and corresponding drive extensions. For test applications, we need to enable PHP_PDO and Php_pdo_sqlite extensions.
Implementing CRUD Operations

The exciting moment has come. We want to implement the CRUD (create, read, update, and delete) operations for the Tbl_user table we just created, which is the most common operation in the actual application. We do not have to bother to write the actual code, here we will use gii--a powerful web-based code generator.

Configuration Gii

In order to use GII, you first need to edit the file webroot/testdrive/protected/main.php, which is a known application configuration file:

Return Array (
 ...
 ') Import ' =>array (
 ' application.models.* ',
 ' application.components.* ',
 ),

 ' modules ' =>array (
 ' Gii ' =>array (
  ' class ' => ' System.gii.GiiModule ', '
  password ' => ' pick up a password ",
 ),
 ),
);

Then, access the URL http://hostname/testdrive/index.php?r=gii. Here we need to enter the password, which is specified in our configuration above.

Generate User Model

After landing, click the link Model generator. It will show the following model generation page,

In the Table Name input box, enter Tbl_user. In the Model Class input box, enter User. Then click the Preview button. This will show you the new file that will be generated. Now click the Generate button. A name of user.php will be generated into the Protected/models directory. As we described later, the User model class allows us to access data table Tbl_user in an object-oriented fashion.

Generate CRUD Code

After creating the model class, we will generate code to perform the CRUD operations. We chose the Crud generator in Gii, as shown below,

In the Model Class input box, enter User. In the Controller ID input box, enter user (lowercase format). Now click the Preview button after the Generate button. The CRUD code generation is complete.

Accessing the CRUD page

Let's look at the results and visit the following URL:

Http://hostname/testdrive/index.php?r=user
This displays a list of the records in a Tbl_user table.

Click on the Create User link on the page and we will be taken to the login page if not logged in. After logging in, we see a form where we can add new users. Complete the form and click the Create button, and if there are any typos, a friendly error message will display and prevent us from saving. Back to the User list page, we should be able to see that the user just added appears in the list.

Repeat these steps to add more users. Note that if a page displays too many user entries, the list page is automatically paginated.

If we log in as an administrator using admin/admin, we can view the user administration page at the following URL:

Http://hostname/testdrive/index.php?r=user/admin
This displays a nice table containing the user entries. We can sort the column by clicking on the cell cells of the header, and it will be automatically paged as the list page.

Implement all of these functions don't let us write a line of code!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.