php 薛强,PHP框架Yii系列教程(一):入门实例

[这里也强烈推荐YII框架,这个年轻的框架由美国华人开发,名叫薛强。Yii Framwork是2009最佳PHP框架。 Yii 是一个基于组件、用于开发大型 Web 应用的高性能 PHP 框架。它将

1准备Yii源码

首先新建helloyii目录作为Web应用的根目录,并添加到Nginx的配置文件中。然后将Yii框架源码部署到helloyii下,目录结构如下:

helloyii/

|-- framework

|-- ……

|-- YiiBase.php

|-- yiic

|-- yii.php

`-- zii

2编写HelloWorld

2.1目录结构

程序目录结构如下:

app/

|-- index.php

`-- protected

|-- controllers

|   `-- HelloController.php

`-- views

`-- hello

`-- result.php

2.2主要代码

index.php

===============================================================================

// change the following paths if necessary

$yii=dirname(__FILE__).'/../framework/yii.php';

// remove the following lines when in production mode

defined('YII_DEBUG') or define('YII_DEBUG',true);

// specify how many levels of call stack should be shown in each logmessage

defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);

require_once($yii);

Yii::createWebApplication()->run();

?>

protected/controllers/HelloController.php

===============================================================================

class HelloController extends CController

{

public function actionSay()

{

$varYii = "hi,yii";

$this->render('result', array('varYii'=>$varYii));

}

}[1环境准备安装Memcached服务端:yum -y installmemcached.x86_64 安装PHP-Memcache扩展:yum -y installphp-pecl-memcache.x86_64 查看PHP-Memcache扩展是否安装成功:php

?>

protected/views/hello/result.php

===============================================================================

echo $varYii;

?>

2.3开始访问

3源码解析

3.1资源映射规则

访问Url经过index.php处理,将请求转发到HelloController的actionSay方法中,然后通过result.php生成最终HTML页面。具体映射关系如下图所示:

Qb2myi.png

注:如果ControllerID和ActionID为默认值site和index的话,则可以通过http://helloyii.com/app/index.php直接访问。请求会转发给SiteController的actionIndex()方法。

4自动生成代码工具

Yii提供了Yiic和Gii两个代码生成工具,可以生成内容更加丰富的实例,下面就来试用一下。

首先切换到/export/data/helloyii,然后执行:

framework/yiicwebapp demo

将会在/export/data/helloyii/demo中自动生成示例程序代码。

现在在浏览器中访问helloyii.com/demo/index.php即可看到成功页面。

B3EvAv.png

5常见问题

访问index.php时,PHP打印警告日志:Warning: date(): It is not safe to rely on the system's timezonesettings. You are *required* to use the date.timezone setting or thedate_default_timezone_set() function…

在php.ini中设置默认时区,或者修改helloyii/demo/protected/views/layouts/main.php:

===============================================================================

……

===============================================================================

参考资料

1一起学Yii—-Hello world

2 yii框架之hello world

3 Creating Your First Yii Application

4应用Yii1.1和PHP5进行敏捷Web开发

[1 MVC架构1.1处理流程一个Web请求在Yii内部的执行流程如下图所示:1.2组件角色组件名角色与责任index.php入口脚本。创建Application的单例对象。application前端控制器。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值