Home > Backend Development > PHP Tutorial > Yii PHP Framework practical introductory tutorial (detailed introduction)_PHP tutorial

Yii PHP Framework practical introductory tutorial (detailed introduction)_PHP tutorial

WBOY
Release: 2016-07-21 15:05:27
Original
857 people have browsed it

说明:因为最近工作工作关系,需要开发一个在Linux下运行的Web Application,需要对现在比较流行的一些PHP框架做一个了解和评估,下面的这篇文章是笔者最近学习一个比较新的PHP Framework的一点经历和操作步骤,因为官方的手册写得比较晦涩(特别是中文的),曾经尝试遍读它那个手册再动手,读了一大半发现仍无法理解,于是干脆先下手为强了,因而也就有了下面的文章。

介绍
Yii 是一个基于组件、纯OOP的、用于开发大型 Web 应用的高性能 PHP 框架。它将 Web 编程中的可重用性发挥到极致,能够显著加速开发进程。Yii适合大流量的应用,如门户、BBS、CMS及B2B系统等,功能丰富,性能优异,不过它的中文文档还不完善,并且有些命令行的操作是针对非Windows用户的,不易理解,所以制作了这篇文档。
下载地址:http://www.yiiframework.com/download/
中文文档地址:http://www.yiiframework.com/doc/guide/zh_cn

配置
下面针对本人的机器相关软件环境及路径做下说明:
Apache2.2.4+PHP5.2.5+MySQL5.1.39
在这里需要说明的是需要在设置Windows环境变量,在Path中添加PHP运行环境所在的目录(如本人在原有配置后加上”;C:/PHP”),因为使用Yii时需要PHP的运行环境。此外,在PHP版本选择时建议不要选择比较高的版本,本人是从PHP5.3.0->PHP5.2.11->PHP5.2.5一路降下来才运行成功的,建议尽量暂时不要使用PHP5.3.0、PHP5.2.11,本人在使用这两个版本过程中经常遇到一个ext目录下的dll文件不能加载的情况,当然你确认自己对PHP的配置相当熟悉的话例外。

因为在Yii中需要开启pdo和pdo_mysql,所以请确保在运行环境所使用的php.ini中取消了extension=php_mysql.dll、extension=php_pdo.dll、extension=php_pdo_mysql.dll的注释。
Apache的网站根路径为D:/wwwroot,在这个根路径下创建一个名为YiiDemo的文件夹,将从网上下载到的Yii压缩包解压之后,拷贝进D:/wwwroot/YiiDemo文件夹,文件结构如下:

注:上图中demosframeworkrequirementsYii压缩包中的文件夹,其它文件和文件夹是本人使用Eclipse时创建的。此外,在D:/wwwroot/YiiDemo/framework文件夹下有一个yiic.bat文件,这个文件可以帮助我们快速生成网站架构和MVC相关的文件。

另外,在本实例中MySQLPHP都是用了utf8编码,不建议使用gb2312编码,能显示的中文字符太少,并且对其它东亚语系不支持,甚至连生僻点的繁体中文都不能显示,而utf8可以解决这个问题。

创建网站初始结构

启动Windows命令行程序(在开始菜单上找到“运行”,然后输入“cmd”并回车),可以看到如下命令行窗口:

在命令行方式下切换到Yiiframework目录下用以执行yiic命令(实际执行的是yiic.bat),如下:

看到如上图所示的信息之后,就可以使用yiic来创建网站结构了,在本例中我们在D:/wwwroot/YiiDemo/framework(注意Apache中网站根路径为D:/wwwroot)下创建网站,网站名字为study,先在D:/wwwroot/YiiDemo/framework下手动创建study这个文件夹,然后使用如下命令创建网站:yiic webapp 网站路径,如下图:

After entering the command to create a website, you will see the prompt as shown in the picture above. After typing ”y”, you will see D:/wwwroot/YiiDemo/study Directory creates the framework structure of the website, as shown below:

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

Generate MVC file
According to the default configuration, the database cannot be used. To get exercises with the database, you need to change the configuration and open D:/wwwroot/YiiDemo/study In the main.php file under the /protected/config folder, just change the configuration in components. The 'db' parameter is commented out. Set the 'db' parameter as follows:

Copy the code The code is as follows:

'db'=>array(
:host=localhost;dbname=study' ,
'username'=>'root',
'password'=>'jeri',
),

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

It should be noted that, as shown in the picture above, because the yiic.bat file is in D:/wwwroot/YiiDemo/ framework folder, and the current working path of the command line is D:/wwwroot/YiiDemo/study, so running yiic It is recommended to use the full path when .

Use the yiic shell command to enter the shell command line, as seen on the command line The input prompt changes to ">>", and typing in the model table name will create the corresponding table's model file. Below the above picture is the situation corresponding to the above picture after successfully using "model user".

You can also use crud (crud are the abbreviations of create/read/update/delete respectively , indicating commonly used add, delete, modify and query database operations ) table name to create corresponding coltroller and view file, as shown below:

For example, use model and crud commands, we can view these files in the browser, such as viewing the userlist table For data, you can enter http://localhost/YiiDemo/study/index.php?r=userlist as shown below Picture:

You can see that although

MySQL and

PHP both use utf8 encoding, but the RealName field in the database cannot be displayed normally because it is Chinese data. This situation occurs because the default character set connection is usedMySQL, when connecting directly to MySQL in PHP, if utf8 encoding, we will make the following settings in the PHP code: mysql_query("set names 'utf8'" );But there is no such opportunity in Yii, We can specify the character set used to connect to the database when setting the database connection string, as follows: Copy code

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 it normally. Of course, you can also change the global configuration, that is, change the
public $charset;
in the CDbConnection.php file under the D:/wwwroot/YiiDemo/framework/db folder. For public $charset='utf8';, can also be displayed normally, as shown in the figure below:


Okay, I haven’t written a line of code so far, but I have completed a website. The general framework, and can initially realize the addition, deletion, modification and query functions. Although it is possible to display Chinese in the database by changing all configurations, this method is not recommended. Many thanks to kingheaven for the tip.

www.bkjia.comtruehttp: //www.bkjia.com/PHPjc/327697.htmlTechArticleNote: Due to recent work, I need to develop a Web Application running under Linux, which is more popular now. To do an understanding and evaluation of some PHP frameworks, the following...
source:php.cn
Statement of this Website
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template