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:
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类。这个 入口脚本大同小异
run();
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) 代码:
这样就完成了Hello,World的MVC模型,运行显示“Hello,World”。
以上就是PHP开发框架Yii Framework教程(1) 第一个应用Hello World的内容,更多相关内容请关注PHP中文网(m.sbmmt.com)!