PHP Yii Framework Getting Started Tutorial, yii Framework tutorial. PHP Yii Framework Getting Started Tutorial, yii Framework usage tutorial install Yii consists of the following two steps: Download Yii Framework from yiiframework.com. Decompress the Yii compressed package to a Yii Framework of We PHP Getting Started Tutorial, yii Framework usage tutorial
Install
The installation of Yii consists of the following two steps:
Download the Yii Framework from yiiframework.com.
Decompress the Yii package to a Web-accessible directory.
Tip | Tip: installation in the Web directory is not required. each Yii application has an entry script, which must be exposed to Web users only. Other PHP scripts (including Yii) should be protected against Web access because they may be exploited by hackers.
Requirement
After installing Yii, you may want to verify whether your server meets the requirements for using Yii. you only need to enter the following URL in your browser to access the requirement detection script:
Http: // hostname/path/to/yii/requirements/index. php
The minimum requirement of Yii is that your Web server supports PHP 5.1.0 or later. Yii has passed the test on Apache HTTP Server on Windows and Linux systems. It should also work properly on other Web servers and platforms that support PHP 5.
Create the first Yii application
To have a preliminary understanding of Yii, we will describe how to build the first Yii application in this section. We will use yiic (command line tool) to create a new Yii application. Gii (a powerful web-based code generator) automatically generates code for specific tasks. Assume that YiiRoot is the installation directory of Yii, and WebRoot is the document root directory of the server.
Run yiic on the command line as follows:
% YiiRoot/framework/yiic webapp WebRoot/testdrive
Note | Note: when running yiic on a MacOS, Linux, or Unix system, you may need to modify the permissions of the yiic file to make it run. You can also run this tool as follows:
% cd WebRoot% php YiiRoot/framework/yiic.php webapp testdrive
This will create a basic Yii application under the WebRoot/testdrive directory. This application has the directory structure required by most Yii applications.
Without writing a line of code, we can access the following URL in the browser to see our first Yii application:
Http: // hostname/testdrive/index. php
As we can see, this application contains three pages: Homepage, contact page, and logon page. The home page displays information about applications and user logon statuses. the contact page displays a contact form for users to enter and submit their inquiries, the logon page allows users to pass authentication and then access authorized content. See the following for more information:
The tree below describes the directory structure of our application.
Testdrive/index. php Web app portal script file index-test.php function test use portal script file assets/contains public resource file css/contains CSS file images/contains image file themes/contains app topic protected/contains subject yiic command line script yiic. the yiic command line script yiic in bat Windows. php yiic command line PHP script commands/contains the custom 'yic' command shell/contains the custom 'yicic shell' command components/contains reusable user component Controller. identity of all php controller classes. the 'Identity 'class config/containing the configuration file console for php authentication. main. php Web application configuration test. the configuration controllers/class file containing the controller SiteController used for php function testing. php default controller class file data/contains sample database schema. mysql. SQL example MySQL database schema. sqlite. SQL example SQLite database testdrive. db example SQLite database file extensions/contains third-party extension messages/contains the translated message models/class file containing the model LoginForm. php 'login' action form model ContactForm. the form model runtime/contains the temporary file tests/contains the test script views/contains the controller view and layout file layouts/contains the Layout view file main. default layout of all views of php column1.php use single-column pages use layout column2.php use double-row pages use layout site/view file pages containing the 'site' controller/contain "static" page about. php "about" page view contact. php 'Contact 'Action view error. php 'error' action View (displaying external errors) index. php 'index' action View login. php 'login' action View system/system view file
Connect to database
Most Web applications are driven by databases, and our testing applications are no exception. To use a database, we first need to tell the application how to connect to it. Modify the application 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 as needed. Note that this SQLite database is included in the application framework we created. The database only contains a table named tbl_user:
CREATE TABLE tbl_user ( id INTEGER NOT NULL PRIMARY KEY AUTOINCREMENT, username VARCHAR(128) NOT NULL, password VARCHAR(128) NOT NULL, email 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 create a database.
Note | Note: to use the Yii database function, we need to enable the PDO extension of PHP and the corresponding driver extension. For testing applications, we need to enable php_pdo and php_pdo_sqlite extensions.
Implement CRUD operations
An exciting moment is coming. We want to implement the CRUD (create, read, update, and delete) operations for the created tbl_user table, which is also the most common operation in practical applications. We don't have to bother writing the actual code. here we will use Gii-a powerful Web-based code generator.
Configure Gii
To use Gii, you must first 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 here', ), ),);
Then, access URL http: // hostname/testdrive/index. php? R = gii. Here we need to enter the password, which is specified in the above configuration.
Generate User model
After logging in, click Model Generator. The following model generation page is displayed,
In the Table Name input box, enter tbl_user. In the Model Class input box, enter User. Click the Preview button. The new file to be generated is displayed here. Click Generate. A file named User. php is generated in the protected/models directory. As described later, the User model class allows us to access the data table tbl_user in an object-oriented manner.
Generate CRUD code
After creating the model class, we will generate the code to execute the CRUD operation. Select Crud Generator in Gii, as shown below,
In the Model Class input box, enter User. In the Controller ID input box, enter user (in lower case ). Click the Preview button after the Generate button. CRUD code generation is complete.
Access the CRUD page
Let's take a look at the results and visit the following URL:
Http: // hostname/testdrive/index. php? R = user
This will display a list of records in the tbl_user table.
Click the Create User link on the page. if you have not logged on, we will be taken to the logon page. After logging on, we can see a form for us to add new users. Complete the form and click the Create button. if any input error occurs, a friendly error prompt will be displayed and will prevent us from saving it. Back to the user list page, we should be able to see that the user just added is displayed in the list.
Repeat the preceding steps to add more users. Note: if too many user entries are displayed on one page, the list page is automatically paged.
If we use admin/admin as the administrator, we can view the user management page at the following URL:
Http: // hostname/testdrive/index. php? R = user/admin
This will display a beautiful table containing user entries. We can click the cells in the header to sort the corresponding columns, and it will automatically pagination like the list page.
Do not write a line of code to implement all these functions!
Articles you may be interested in:
- Build a PHP Yii Framework and related testing environment on Mac OS
- How to install php framework Yii in win7
- Example of rewrite rules for configuring the Yii and CakePHP frameworks of PHP in Nginx
- Describes the configuration and usage of logs in the Yii Framework of PHP.
- The following describes how to run yii2.0 in the php command line.
- Yii connection, modify MySQL database and phpunit test connection
- Php yii Framework Development Tips: rules custom validation rules in model (models)
- PHP Yii Framework-table verification rules
- PHP generates ICO icons based on yii Framework
- List the development advantages of the PHP Yii 2 framework
The Yii Framework is downloaded from yiiframework.com. Decompress the Yii package to a We...