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:
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, 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:
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.