Yii PHP Framework practical getting started tutorial (details)

Source: Internet
Author: User

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:MediumDemos,Framework,RequirementsYesYiiThe folder in the compressed package. Other files and folders are used by the user.EclipseCreated. In additionD:/wwwroot/YiiDemo/frameworkThere isYiic. batFile, which can help us quickly generate the website architecture andMVCRelated Files.

In additionMySQLAndPHPAll are usedUtf8Encoding, not recommendedGb2312It can contain too few Chinese characters and is not supported by other East Asian languages. It cannot even display traditional Chinese characters with uncommon Chinese characters.Utf8This problem can be solved.

Create initial website structure

StartWindowsCommand Line Program (find "run" on the Start menu and enter"CmdAnd press Enter) to see the following command line window:

SwitchYiiOfFrameworkDirectory for executionYiicCommand (actually executed isYiic. bat), As follows:

You can useYiicCreate a website structure. In this exampleD:/wwwroot/YiiDemo/framework(Note:ApacheThe root path of the website isD:/wwwroot).Study, First inD:/wwwroot/YiiDemo/frameworkManually created underStudyThis folder, and then use the following command to create a website:Yiic webappWebsite path, such:

After entering the command to create a website, you will see the prompt as shown in. Type"Y"ThenD:/wwwroot/YiiDemo/studyDirectory to create the framework of the website, 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:
Copy codeThe 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, becauseYiic. batFile inD:/wwwroot/YiiDemo/frameworkFolder, and the current working path of the command line isD:/wwwroot/YiiDemo/study, So you are runningYiicFull path is recommended.

UseYiic shellCommand to enterShellIn the command line, the input prompt displayed in the command line is changed to">", TypeModelThe table name will create the corresponding tableModelFile.Model user"You can see the corresponding situation after the success.

You can also useCrud (crudThey areCreate/read/update/deleteCommon Operations for adding, deleting, modifying, and querying databases)Table NameColtrollerAndViewFile, as shown in:

For exampleUser,UserlistAnd so onModelAndCrudAfter the command, we can view these files in the browser, as shown inUserlistData in the table, which can be entered in the browserHttp: // localhost/YiiDemo/study/index. php? R = userlistSee the following figure:

You can see that althoughMySQLAndPHPAll are usedUtf8Encoding, butRealNameThe field cannot be properly displayed because it is a Chinese data. This is because the default character set connection is used.MySQL, InPHPDirect ConnectionMySQLIfUtf8Encoding, we willPHPMake the following settings in the Code:Mysql_query ("set names 'utf8 '");HoweverYiiDoes 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:

Copy codeThe 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.