Yii PHP Framework getting started tutorial

Source: Internet
Author: User

Yii PHP Framework getting started tutorial

Note: Due to recent work, you need to develop a Web Application executed in Linux and evaluate some of the PHP frameworks that are more popular than idea, in the following article, I recently learned a little bit about the experience and operation steps of a newer PHP Framework than NLP, because the official hand tips are rather obscure (especially Chinese ), in the past, I tried to read the hacker again, but I still couldn't understand it after reading more than half of it. So I started my work first, so I had the following articles.

Introduction

Yii is a high-performance PHP framework based on components and pure OOP for developing large Web applications. It brings the reusability in Web programming to the extreme and can significantly accelerate 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 difficult 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 must add the PHP Execution Environment folder to the Path when setting the Windows environment variable (for example, add "; C:/PHP" after the original configuration "), the execution environment of PHP is required when Yii is used. In addition, we recommend that you do not select a version later than PHP5.3.0 when selecting the PHP version number. You can only successfully execute the PHP version after downgrading PHP5.3.0-> PHP5.2.11-> PHP5.2.5, we recommend that you do not use PHP5.3.0 or PHP5.2.11 temporarily. When using these two versions, I often encounter a situation where the dll files in the ext folder 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, ensure that extension = php_mysql.dll, extension = php_pdo.dll, and extension = php_pdo_mysql.dll are canceled in the php. ini used in the execution environment.

Apache's site root path is D:/wwwroot. Create a directory named YiiDemo under this root path, decompress the downloaded Yii package from the Internet, and copy it to D: /wwwroot/YiiDemo directory. The file structure is as follows:

Note: demos, framework, and requirements are the directories in the Yii compressed package. Other files and directories are created when I use Eclipse. In addition, there is a yiic. bat file under the D:/wwwroot/YiiDemo/framework directory. This file can help us to generate site architecture and MVC-related files at high speed.

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 the problem.

Create initial site structure

Start the Windows Command Line Program (find "execute" on the Start menu, then enter "cmd" and press Enter). The following command line form is displayed:

Switch to the yiic framework folder in the command line mode to run the yiic command (the actual operation is yiic. bat), for example:

After you see the information, you can use yiic to create the site structure. In this example, we will go to D: /wwwroot/YiiDemo/framework (note that the root path of the site in Apache is D:/wwwroot) to create a site. The site name is study, first in D: manually create the study directory under/wwwroot/YiiDemo/framework, and then use the following command to create the site: yiic webapp site path, for example:

After entering the site creation command, you will see the prompt as shown in the figure. After typing "y", you will create the framework structure of the site in the D:/wwwroot/YiiDemo/study folder, as shown in the following figure:

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 Directory D:/wwwroot/YiiDemo/study/protected/config. in the PHP file, you can change the configuration in components. The number of 'db' shards is staring, and the number of 'db' shards is set 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 based on your actual situation.

Continue to use the yiic command line tool, switch the working path to the D:/wwwroot/YiiDemo/study directory through cd, and then create the model and view files in the command line. For example:

Note that, as you can see, yiic. the bat file is in the Directory D:/wwwroot/YiiDemo/framework, and the current command line works in the path D:/wwwroot/YiiDemo/study, therefore, we recommend that you use full path when executing yiic.

Run the yiic shell command to access 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 situation after "model user" is used successfully.

You can also use crud (crud is the abbreviation of create/read/update/delete respectively, indicating the frequently used operations for adding, deleting, modifying, and querying databases) to create the corresponding coltroller and view files with the table name, as shown in the following figure:

After using model and crud commands such as table user and userlist, we can view these files in a 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 properly displayed due to Chinese data. This is because the handler uses the default character set to connect to MySQL, when MySQL is directly connected in PHP, assuming utf8 encoding is used, we will make the following settings in PHP code: mysql_query ("set names 'utf8 '"); however, Yii does not provide this opportunity,

We can specify the character set used to connect to the database when setting the database connection string, for example:

'Db' => array (

'Connectionstring' => 'mysql: host = localhost; dbname = study ',

'Username' => 'root ',

'Password' => 'jeri ',

'Charset' => 'utf8', // use charset as utf8

),

This will display normally. Of course, you can also change the global configuration, that is, change the content in the CDbConnection. php file under the D:/wwwroot/YiiDemo/framework/db directory.

Public $ charset;

Public $ charset = 'utf8'; can also be displayed normally, as shown in the following figure:

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 the scheme can display Chinese characters in the database by changing all configurations, it is not recommended that the scheme be used. Thank you for your prompt.

Yii User Login Mechanism

Introduce js and css files in Yii

Incomplete Yii Solution

Yii CGridView basic usage

Yii framework distributed cache Implementation Scheme

Yii details: click here
Yii: click here

This article permanently updates the link address:

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.