


PHP development framework Yii Framework tutorial (1) The first application Hello World
Jan 21, 2017 am 09:24 AMYii 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
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
這個(gè)目錄結(jié)構(gòu)可以通過Yii自帶的工具來創(chuàng)建缺省的文件建立第一個(gè) Yii 應(yīng)用。
對(duì)于Hello World項(xiàng)目來說,沒有必要這么復(fù)雜,我們只需創(chuàng)建 protected \controllers 目錄以存放SiteController.php。
每個(gè)Yii應(yīng)用都有的入口腳本,可以理解為C#的Program類。這個(gè) 入口腳本大同小異
<?php // 包含Yii引導(dǎo)文件 //require_once(dirname(__FILE__).'/../../framework/yii.php'); $yii='C:/yiiframework/yii.php'; // 發(fā)布應(yīng)用時(shí),去掉下面代碼避免產(chǎn)生調(diào)試信息 defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); // 創(chuàng)建一個(gè)應(yīng)用實(shí)例并執(zhí)行 Yii::createWebApplication()->run();
<?php // 包含Yii引導(dǎo)文件 //require_once(dirname(__FILE__).'/../../framework/yii.php'); $yii='C:/yiiframework/yii.php'; // 發(fā)布應(yīng)用時(shí),去掉下面代碼避免產(chǎn)生調(diào)試信息 defined('YII_DEBUG') or define('YII_DEBUG',true); require_once($yii); // 創(chuàng)建一個(gè)應(yīng)用實(shí)例并執(zhí)行 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'; } }
此時(shí)再運(yùn)行應(yīng)用,可以在瀏覽器中顯示“Hello,World”。 目前沒有使用MVC模型直接在Controller 使用echo 打印出“Hello,World”, 下面稍微修改一下代碼,創(chuàng)建一個(gè)簡(jiǎn)單的View。
View缺省目錄為protected 目錄下的views 子目錄,和Controller類對(duì)于,比如SiteController對(duì)應(yīng)到Views目錄下的site子目錄,和Asp.Net一樣,Yii的View(對(duì)應(yīng)到Asp.Net的Page類)也可以使用MasterPage,Yii應(yīng)用成為L(zhǎng)ayout,缺省Layout存放在views的layouts 子目錄。
修改SiteController的actionIndex 方法,改為:
public function actionIndex() { $this->render("index"); }
public function actionIndex() { $this->render("index"); }
View 視圖是一個(gè)包含了主要的用戶交互元素的PHP腳本.他可以包含PHP語句,但是我們建議這些語句不要去改變數(shù)據(jù)模型,且最好能夠保持其單純性(單純作為視圖)。為了實(shí)現(xiàn)邏輯和界面分離,大段的邏輯應(yīng)該被放置于控制器或模型中,而不是視圖中,視圖有一個(gè)名字,當(dāng)渲染(render)時(shí),名字會(huì)被用于識(shí)別視圖腳本文件。
actionIndex 通過render 方法來顯示一個(gè)View,對(duì)應(yīng)到views->site 目錄下的 index.php 。render 缺省使用views ->layouts 下的 main.php 作為 Layout (布局,MasterPage)
布局是一種用來修飾視圖的特殊的視圖文件.它通常包含了用戶界面中通用的一部分視圖.例如:布局可以包含header和footer的部分,然后把內(nèi)容嵌入其間.
......header here......
......footer here......
其中的 $content 則儲(chǔ)存了內(nèi)容視圖的渲染結(jié)果.
來看一下View是目錄下的index.php (View) 代碼:
<?php echo "Hello,World!"; ?>
<?php echo "Hello,World!"; ?>
這樣就完成了Hello,World的MVC模型,運(yùn)行顯示“Hello,World”。
以上就是PHP開發(fā)框架Yii Framework教程(1) 第一個(gè)應(yīng)用Hello World的內(nèi)容,更多相關(guān)內(nèi)容請(qǐng)關(guān)注PHP中文網(wǎng)(miracleart.cn)!

Hot AI Tools

Undress AI Tool
Undress images for free

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Clothoff.io
AI clothes remover

Video Face Swap
Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Whenever your Windows 11 or Windows 10 PC has an upgrade or update issue, you will usually see an error code indicating the actual reason behind the failure. However, sometimes confusion can arise when an upgrade or update fails without an error code being displayed. With handy error codes, you know exactly where the problem is so you can try to fix it. But since no error code appears, it becomes challenging to identify the issue and resolve it. This will take up a lot of your time to simply find out the reason behind the error. In this case, you can try using a dedicated tool called SetupDiag provided by Microsoft that helps you easily identify the real reason behind the error.

.NET Framework 4 is required by developers and end users to run the latest versions of applications on Windows. However, while downloading and installing .NET Framework 4, many users complained that the installer stopped midway, displaying the following error message - " .NET Framework 4 has not been installed because Download failed with error code 0x800c0006 ". If you are also experiencing it while installing .NETFramework4 on your device then you are at the right place
![SCNotification has stopped working [5 steps to fix it]](https://img.php.cn/upload/article/000/887/227/168433050522031.png?x-oss-process=image/resize,m_fill,h_207,w_330)
As a Windows user, you are likely to encounter SCNotification has stopped working error every time you start your computer. SCNotification.exe is a Microsoft system notification file that crashes every time you start your PC due to permission errors and network failures. This error is also known by its problematic event name. So you might not see this as SCNotification having stopped working, but as bug clr20r3. In this article, we will explore all the steps you need to take to fix SCNotification has stopped working so that it doesn’t bother you again. What is SCNotification.e

Microsoft Windows users who have installed Microsoft.NET version 4.5.2, 4.6, or 4.6.1 must install a newer version of the Microsoft Framework if they want Microsoft to support the framework through future product updates. According to Microsoft, all three frameworks will cease support on April 26, 2022. After the support date ends, the product will not receive "security fixes or technical support." Most home devices are kept up to date through Windows updates. These devices already have newer versions of frameworks installed, such as .NET Framework 4.8. Devices that are not updating automatically may

In the current information age, big data, artificial intelligence, cloud computing and other technologies have become the focus of major enterprises. Among these technologies, graphics card rendering technology, as a high-performance graphics processing technology, has received more and more attention. Graphics card rendering technology is widely used in game development, film and television special effects, engineering modeling and other fields. For developers, choosing a framework that suits their projects is a very important decision. Among current languages, PHP is a very dynamic language. Some excellent PHP frameworks such as Yii2, Ph

The Yii framework is a high-performance, scalable, and secure PHP framework. It is an excellent development tool that allows developers to build complex web applications quickly and efficiently. Here are a few reasons why Yii framework is better to use than other frameworks. The high-performance Yii framework uses some advanced technologies, such as lazy loading and automatic classloading, which make the performance of the Yii framework higher than that of many other frameworks. It also mentions

It's been a week since we talked about the new safe mode bug affecting users who installed KB5012643 for Windows 11. This pesky issue didn't appear on the list of known issues Microsoft posted on launch day, thus catching everyone by surprise. Well, just when you thought things couldn't get any worse, Microsoft drops another bomb for users who have installed this cumulative update. Windows 11 Build 22000.652 causes more problems So the tech company is warning Windows 11 users that they may experience problems launching and using some .NET Framework 3.5 applications. Sound familiar? But please don't be surprised

The Yii framework is an open source PHP Web application framework that provides numerous tools and components to simplify the process of Web application development, of which data query is one of the important components. In the Yii framework, we can use SQL-like syntax to access the database to query and manipulate data efficiently. The query builder of the Yii framework mainly includes the following types: ActiveRecord query, QueryBuilder query, command query and original SQL query
