Home > Article > Backend Development > PHP development framework Yii Framework tutorial (1) The first application Hello World
Yii Framework tutorials can be found in the official Chinese documentation, so why write this development tutorial? The purpose of this tutorial is to help Windows desktop application or ASP.NET programmers quickly master the PHPYii Framework application framework through different perspectives (mainly from the perspective of C++ and C# programmers who develop Windows applications). An important benefit of using PHP to develop web applications is that it can be applied to various operating systems (Unix, Windows, Mac OS), unlike Asp.Net, which can generally only be applied to Windows operating systems. Using PHP+Apache+MySQL (XMAP/LAMP) you can defeat almost all the invincible players in the world :-).
The operating system used in this tutorial is Windows, and the development IDE is VS.PHP. The reason why this development environment is used is because VS.PHP uses Visual Studio as the IDE, which is familiar to Visual Studio developers. And it can be used to develop and debug C# and PHP applications at the same time. Yii Framework itself has nothing to do with IDE. You can use your favorite PHP development tools to develop Yii applications (such as Eclipse). For an introduction to VS.PHP, see VS.PHP + YiiFramework combination to develop PHP applications. The knowledge about Yii Framework in this tutorial has nothing to do with developing IDE. It can be applied to various development environments. You can choose the development environment you like.
Before creating the first application, you need to download the Yii development package. You can download it from the Yii website http://www.yiiframework.com/download/. The current version is 1.1.12. After downloading, directly Unzip it to the C: root directory for convenience:
<?php print "Hello, World!"; ?>
<?php print "Hello, World!"; ?>Press F5 to run the program, VS.PHP opens the Quesheng browser and displays "Hello, World!". But this is not a Yii application! ! ! , we have not used Yii Web application framework, Yii Framework is a pure object-oriented application framework. The Application class of its Web program is the CWebApplication class. And adopts the MVC model. The diagram below shows the static structure of the Yii application The following figure shows the typical workflow of a Yii application when processing user requests
The user issued a request to access the URL http://www.example.com/index.php?r=post/show&id=1, and the web server passed Execute the entry script index.php to process this request. The entry script creates an application instance and executes it. The application obtains the details of the user's request from an application component called request. The application determines the requested controller and action with the help of an application component called urlManager. In this example, the controller is post, which represents the PostController class; the action is show, whose actual meaning is determined by the controller. The application creates an instance of the requested controller to further handle the user request. The controller determines the action show points to a method named actionShow in the controller class. It then creates and maintains filters associated with the action (e.g. access control, benchmarking). If allowed by the filter, the action will be executed. The action reads a Post model with ID 1 from the database. The action renders a view named show through the Post model. The view reads and displays the properties of the Post model. The view executes some small objects. The rendering result of the view is inserted into a layout. The action completes rendering of the view and presents it to the user. The Yii application itself has many configurations, such as the selected Controller, whether the action uses Log files, etc. For a simple application like Hello, World, all default values are used. The default Controller is SiteController, and the Action is indexAction. That is to say, for example, if your website is www.guidebee.com, if you use Yii Framework, when the user requests www.guidebee.com, the CWebApplication of the Yii application will create a SiteController class Instance and call the indexAction method of SiteController (equivalent to the main method of Program in C# Console application). The Yii project uses different directories to store Controller, Action, View, Layout, etc. Its default directory structure is as follows
testdrive/
index.php use use using using use using ‐ ’ s ’ s through using ‐ ‐ ‐ ‐ ‐ – Contains CSS files
IMAGES/ Including picture files
Themes/ Including application themes
Protected/ Contains protected application files
yiic yiic command line script
yiic.bat Windows
yiic.php Yiic command line PHP script
Commands/ contains customized 'yiic' command
shell/ contains custom 'yiic shell' command
components/ contains reusable user components
Controller .php The base class for all controller classes
Identity.php The 'Identity' class used for authentication
config/ Contains the configuration file
console.php Console application configuration
main.php Web application configuration
use using using using . schema.mysql.sql Example MySQL database
schema.sqlite.sql Example SQLite database
testdrive.db Example SQLite database file
extensions/ Contains third-party extensions
Messages/ Contains translations Past messages
Models/ Contains models Class file
LoginForm.php 'login' action form model
ContactForm.php 'contact' action form model
runtime/ Contains temporarily generated files
tests/ tests/ Contains test script
Views/ View and layout files of the controller
layouts/ Including layout view files
main.php The default layout of all views
column1.php uses the layout used by a single page
column2.php. The layout used by the column's pages
through out ‐ ‐ through ’ ’ s through through ‐ ‐ ‐ ‐ ‐ to use contact.php 'contact ' The view of the action
error.php The view of the 'error' action (shows external errors)
index.php '''''''''''s' view of the action's ’‐’action's'''‐‐through''''‐action's view
/ Contains system view files
这个目录结构可以通过Yii自带的工具来创建缺省的文件建立第一个 Yii 应用。
对于Hello World项目来说,没有必要这么复杂,我们只需创建 protected \controllers 目录以存放SiteController.php。
每个Yii应用都有的入口脚本,可以理解为C#的Program类。这个 入口脚本大同小异
<?php // 包含Yii引导文件 //require_once(dirname(__FILE__).'/../../framework/yii.php'); $yii='C:/yiiframework/yii.php'; // 发布应用时,去掉下面代码避免产生调试信息 defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); // 创建一个应用实例并执行 Yii::createWebApplication()->run();
<?php // 包含Yii引导文件 //require_once(dirname(__FILE__).'/../../framework/yii.php'); $yii='C:/yiiframework/yii.php'; // 发布应用时,去掉下面代码避免产生调试信息 defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); // 创建一个应用实例并执行 Yii::createWebApplication()->run();
前面说过Yii的缺省Controller为SiteController,缺省Action为actionIndex, 因此HelloWorld的SiteController代码如下
/** * SiteController is the default controller to handle user requests. */ class SiteController extends CController { /** * Index action is the default action in a controller. */ public function actionIndex() { echo 'Hello World'; } }
/** * SiteController is the default controller to handle user requests. */ class SiteController extends CController { /** * Index action is the default action in a controller. */ public function actionIndex() { echo 'Hello World'; } }
此时再运行应用,可以在浏览器中显示“Hello,World”。 目前没有使用MVC模型直接在Controller 使用echo 打印出“Hello,World”, 下面稍微修改一下代码,创建一个简单的View。
View缺省目录为protected 目录下的views 子目录,和Controller类对于,比如SiteController对应到Views目录下的site子目录,和Asp.Net一样,Yii的View(对应到Asp.Net的Page类)也可以使用MasterPage,Yii应用成为Layout,缺省Layout存放在views的layouts 子目录。
修改SiteController的actionIndex 方法,改为:
public function actionIndex() { $this->render("index"); }
public function actionIndex() { $this->render("index"); }
View 视图是一个包含了主要的用户交互元素的PHP脚本.他可以包含PHP语句,但是我们建议这些语句不要去改变数据模型,且最好能够保持其单纯性(单纯作为视图)。为了实现逻辑和界面分离,大段的逻辑应该被放置于控制器或模型中,而不是视图中,视图有一个名字,当渲染(render)时,名字会被用于识别视图脚本文件。
actionIndex 通过render 方法来显示一个View,对应到views->site 目录下的 index.php 。render 缺省使用views ->layouts 下的 main.php 作为 Layout (布局,MasterPage)
布局是一种用来修饰视图的特殊的视图文件.它通常包含了用户界面中通用的一部分视图.例如:布局可以包含header和footer的部分,然后把内容嵌入其间.
......header here......
......footer here......
其中的 $content 则储存了内容视图的渲染结果.
来看一下View是目录下的index.php (View) 代码:
<?php echo "Hello,World!"; ?>
<?php echo "Hello,World!"; ?>
这样就完成了Hello,World的MVC模型,运行显示“Hello,World”。
以上就是PHP开发框架Yii Framework教程(1) 第一个应用Hello World的内容,更多相关内容请关注PHP中文网(www.php.cn)!