A practical introductory course for the YII PHP Framework (detailed) _php tips

Source: Internet
Author: User
Tags php framework yii
Description:Because of the recent work and work relationship, need to develop a Linux run under the Web application, need to do some of the current more popular PHP framework to do an understanding and evaluation, the following article is the author of a recent study of a relatively new PHP framework experience and procedures , because the Official handbook is more obscure (especially in Chinese), once tried to read it that manual again, read the half found still unable to understand, so simply first to the strong, so there is the following article.

Introduce
Yii is a component-based, pure OOP, high-performance PHP framework for developing large WEB applications. It will maximize the reusability of WEB programming and can significantly accelerate the development process. Yii suitable for large flow of applications, such as portals, BBS, CMS and business-to-business Systems, features rich, excellent performance, but its Chinese documents are not perfect, and some command line operation is for non-Windows users, not easy to understand, so the production of this document.
Download Address: http://www.yiiframework.com/download/
Chinese document Address: HTTP://WWW.YIIFRAMEWORK.COM/DOC/GUIDE/ZH_CN

Configuration
The following for my machine-related software environment and path to do the following instructions:
apache2.2.4+php5.2.5+mysql5.1.39
What needs to be explained here is the need to set Windows environment variables in the path to add the PHP running environment in the directory (such as I added after the original configuration); C:/php "), because using YII requires the operating environment of PHP. In addition, the choice of PHP version is not recommended to choose a relatively high version, I was from the php5.3.0->php5.2.11->php5.2.5 all the way down to run successfully, the proposal as far as possible not to use PHP5.3.0, PHP5.2.11, I often encountered in the use of these two versions of the EXT directory DLL file can not be loaded, of course, you confirm your PHP configuration is quite familiar with the exception.

Because PDO and pdo_mysql need to be turned on in Yii, make sure that Extension=php_mysql.dll, Extension=php_pdo.dll, Extension=php_ are canceled in the php.ini used in the running environment Pdo_mysql.dll's comment.
Apache's Web site root path is d:/wwwroot, under this root path to create a folder named Yiidemo, will be downloaded from the Internet to the YII compression package after decompression, copied into the D:/wwwroot/yiidemo folder, the file structure is as follows:

Note: the Demos,frameworkandrequirements are the folders in the Yii compressed package, and other files and folders are used by me. created while in Eclipse. In addition, there is a Yiic.bat file under the D:/wwwroot/yiidemo/framework folder that can help us quickly generate Web site architecture and MVC -related files.

In addition, in this example MySQL and PHP are used UTF8 encoding, do not recommend the use of gb2312 encoding, can display too little Chinese characters, and other East Asian languages do not support, Even the traditional Chinese can not be displayed, and UTF8 could solve the problem.

Create Site initial structure

To start the Windows command-line program (find run on the Start menu, and then enter "cmd" and return), you can see the following command Line window:

Switch to the framework directory of YII at the command line to execute the YIIC command (the actual execution is Yiic.bat) as follows:

Once you see the information shown above, you can use YIIC to create the site structure, in this case we are in d:/wwwroot/yiidemo/framework(note Apache Site Root path is d:/wwwroot) under Create a Web site, the site name is study, first manually created under D:/wwwroot/yiidemo/framework study this folder, and then use the following command to create the site:YIIC webapp site path, as shown in the following figure:

After you enter the command to create a Web site, you will see the prompt shown in the figure above, and after you type "Y" you will create a frame structure for the site in the d:/wwwroot/yiidemo/study directory, as shown in the following illustration:

According to my machine configuration, now you can see the prototype of the YII framework shows that the Web site is: http://localhost/YiiDemo/study/index.php.

Generate MVC File
The default configuration does not use the database, to get to practice with the database, you need to change the configuration, open d:/wwwroot/yiidemo/study/protected/ Config folder, change the configuration in components, the ' db ' parameter is commented out, and the ' db ' parameter is set as follows: main.php

Copy Code code as follows:

' DB ' =>array (
' connectionString ' => ' mysql:host=localhost;dbname=study ',
' username ' => ' root ',
' Password ' => ' Jeri ',
),

After the storage can be connected to the MySQL database, the actual use should be based on their actual situation configuration.
This will continue using the YIIC command-line tool, switching the working path through the CD to the D:/wwwroot/yiidemo/study folder, and then creating the model and view files on the command line. As follows:

Note that as shown in the figure above, because the yiic.bat file is under the d:/wwwroot/yiidemo/framework folder, and the current command line's working path is d:/wwwroot/yiidemo/ Study, so it is recommended that you use a full path when running YIIC.

You can go to the shell command line using the YIIC shell command, and the input prompt that you see at the command line changesto ">>", and the name of the Model table creates the corresponding table model file, the image below is the useof "model user" after the success of the above map can see the corresponding situation.

You can also create corresponding coltroller using crud (Crud is abbreviated as Create/read/update/delete, representing common additions and deletions to database operations) table names and view files, as shown in the following illustration:

For example, after using model and CRUD commands for table user, userlist, and so on, We can view these files in the browser, such as viewing userlist The data in the table, you can enter http://localhost/YiiDemo/study/index.php?r=userlist in the browser and see the figure shown below:

You can see that although both MySQL and PHP are UTF8 encoded, the Realname fields in the database are not displayed properly because they are Chinese data. This happens because the default character set is used to connect MySQL, and If you use the UTF8 encoding in PHP when you connect directly to MySQL, we will The code is set as follows:mysql_query ("Set names ' UTF8 '"); But there is no such opportunity in Yii,

We can specify the character set used by the connection database when setting the database connection string, as follows:

Copy Code code as follows:

' DB ' =>array (
' connectionString ' => ' mysql:host=localhost;dbname=study ',
' username ' => ' root ',
' Password ' => ' Jeri ',
' CharSet ' => ' UTF8 ',//use charset as UTF8
),

This will show up normally. Of course, you can change the global configuration, that is, change the cdbconnection.php file in the d:/wwwroot/yiidemo/framework/db folder
Public $charset;
change to public $charset = ' UTF8 ';, as shown in the following illustration:


Well, not write a line of code, but has completed a Web site's general framework, and can be initially implemented to change the function of the check. Although you can also implement the Chinese in the display database by changing all configurations, this is not recommended. Thank you very much for Kingheaven's tips here.

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.