YiiPHPFramework practical Getting Started Tutorial (details)

Source: Internet
Author: User
This article provides a detailed analysis of the YiiPHPFramework practical Getting Started Tutorial. For more information, see Note:Due to recent work relationships, you need to develop a Web Application running in Linux and evaluate some popular PHP frameworks, the following article describes some of the experiences and Operation steps of a relatively new PHP Framework recently, because the official manual is rather obscure (especially Chinese ), I tried to read the manual again, but I still couldn't understand it after reading more than half of the manual. so I started my work first, so I had the following article.

Introduction
Yii is a high-performance PHP framework based on components and pure OOP for developing large Web applications. It brings the reusability of Web programming to the extreme and significantly accelerates the development process. Yii is suitable for applications with large traffic volumes, such as portals, BBS, CMS, and B2B systems. Yii has rich functions and excellent performance, but its Chinese documents are not complete yet, in addition, some command line operations are intended for non-Windows users and are not easy to understand. Therefore, this document is created.
: Http://www.yiiframework.com/download/
Chinese document address: http://www.yiiframework.com/doc/guide/zh_cn

Configuration
The following describes the software environment and path of your machine:
Apache2.2.4 + PHP5.2.5 + MySQL5.1.39
Note that you need to add the directory where the PHP runtime environment is located in the Path when setting the Windows environment variable (for example, add "; C:/PHP" after the original configuration "), because the PHP runtime environment is required when Yii is used. In addition, we recommend that you do not select a higher version when selecting a PHP version. you can run the PHP version only after you downgrade PHP5.3.0 to PHP5.2.11, we recommend that you do not use PHP5.3.0 or PHP5.2.11 for the time being. when using these two versions, I often encounter that dll files in the ext directory cannot be loaded, of course, you are sure that you are quite familiar with PHP configurations.

Because pdo and pdo_mysql must be enabled in Yii, make sure that extension = php_mysql.dll, extension = php_pdo.dll, and extension = php_pdo_mysql.dll are removed from the php. ini file used in the runtime environment.
The root path of Apache is D:/wwwroot. in this root path, create a folder named YiiDemo, decompress the downloaded Yii package from the Internet, and copy it to D: /wwwroot/YiiDemo folder. the file structure is as follows:

Note:Demos, framework, and requirements in the Yii compressed package. other files and folders are created when I use Eclipse. In addition, there is a yiic. bat file in the D:/wwwroot/YiiDemo/framework folder. This file can help us quickly generate website architecture and MVC-related files.

In addition, MySQL and PHP both use utf8 encoding in this example. We do not recommend using gb2312 encoding. the Chinese characters that can be displayed are too few and are not supported by other East Asian languages, even traditional Chinese characters with uncommon features cannot be displayed, and utf8 can solve this problem.

Create initial website structure

Start the Windows command line program (find "run" on the Start menu, enter "cmd", and press enter). the following command line window is displayed:

Switch to the yiic framework directory in the command line mode to execute the yiic command (yiic. bat is actually executed), as follows:

After you see the information shown in, you can use yiic to create a website structure. In this example, we use D:/wwwroot/YiiDemo/framework (note that the website root path in Apache is D: /wwwroot) create a website named study. first, manually create the study folder under D:/wwwroot/YiiDemo/framework, and then use the following command to create a website: yiic webapp website path, such:

After entering the command to create a website, you will see the prompt as shown in. after entering "y", you will create the framework structure of the website in the D:/wwwroot/YiiDemo/study directory, as shown in:

According to my machine configuration, now we can see the prototype of the Yii Framework. the URL is: http: // localhost/YiiDemo/study/index. php.

Create an MVC file
The database cannot be used according to the default configuration. to obtain the exercises with the database, you need to change the configuration and open main in the folder D:/wwwroot/YiiDemo/study/protected/config. in the php file, modify the configuration in components. comment out the 'DB' parameter and set the 'DB' parameter as follows:
The code is as follows:
'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = study ',
'Username' => 'root ',
'Password' => 'jeri ',
),

After saving it, you can connect to the MySQL database. you need to configure it according to your actual situation.
Continue to use the yiic command line tool, switch the working path to the D:/wwwroot/YiiDemo/study folder through cd, and then create the model and view files in the command line. As follows:

Note that, as shown in, yiic. the bat file is in the D:/wwwroot/YiiDemo/framework folder, while the current command line works in the path D:/wwwroot/YiiDemo/study, therefore, full path is recommended when running yiic.

Run the yiic shell command to go to the shell command line. The input prompt displayed on the command line is ">". if you type the model table name, the model file of the corresponding table will be created, the following figure shows the corresponding situation after "model user" is used successfully.

You can also use crud (crud is the abbreviation of create/read/update/delete, indicating common database operations for addition, deletion, modification, and query) to create the corresponding coltroller and view files by table name, as shown in:

For example, after using the model and crud commands for tables such as user and userlist, we can view these files in the browser. for example, to view the data in the userlist table, you can enter http in the browser: // localhost/YiiDemo/study/index. php? R = userlist. The following figure is displayed:

We can see that although MySQL and PHP both use utf8 encoding, the RealName field in the database cannot be normally displayed due to Chinese data. this is because MySQL is connected using the default character set, when MySQL is directly connected in PHP, if utf8 encoding is used, we will make the following settings in the PHP code: mysql_query ("set names 'utf8 '"); however, Yii does not provide such an opportunity,

We can specify the character set used to connect to the database when setting the database connection string, as shown below:

The code is as follows:
'DB' => array (
'Connectionstring' => 'MySQL: host = localhost; dbname = study ',
'Username' => 'root ',
'Password' => 'jeri ',
'Charset' => 'utf8', // use charset as utf8
),

This will display normally. You can also change the global configuration, that is, in the CDbConnection. php file in the D:/wwwroot/YiiDemo/framework/db folder
Public $ charset;
Public $ charset = 'utf8'; can also be displayed normally, as shown in:


Now, no code is written, but the general framework of a website has been completed, and the addition, deletion, modification, and query functions can be initially implemented. Although you can display Chinese characters in the database by changing all configurations, this method is not recommended. Thank you for your prompt.

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.