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

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

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

<?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

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

<?php

class HelloController extends CController

{

public function actionSay()

{

$varYii = "hi,yii";

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

}

}

?>

protected/views/hello/result.php

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

<?php

echo $varYii;

?>

2.3开始访问

现在可以直接访问了:http://helloyii.com/app/index.php?r=hello/say

3源码解析

3.1资源映射规则

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


注:如果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即可看到成功页面。


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:

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

……

<?phpdate_default_timezone_set('Europe/Athens'); ?>

<div id="footer">

Copyright &copy; <?phpecho date('Y'); ?> by My Company.<br/>

All Rights Reserved.<br/>

<?php echo Yii::powered();?>

</div><!-- footer -->

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

参考资料

1一起学Yii—-Hello world

http://istrone.com/?p=451

2 yii框架之hello world

http://513394217.blog.163.com/blog/static/10979118620124288721586/

3 Creating Your First Yii Application

http://www.yiiframework.com/doc/guide/1.1/en/quickstart.first-app

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

http://yiibook.com/book/agile_web_application_development_with_yii1.1_and_php5/chapter-2

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Yii 2.0 权威指南 本教程的发布遵循 Yii 文档使用许可. 版权所有 2014 (c) Yii Software LLC. 介绍 已定稿 关于 Yii 已定稿 从 Yii 1.1 升级 入门 已定稿 安装 Yii 已定稿 运行应用 已定稿 第一次问候 已定稿 使用 Forms 已定稿 玩转 Databases 已定稿 用 Gii 生成代码 已定稿 更上一层楼 应用结构 已定稿 结构概述 已定稿 入口脚本 已定稿 应用 已定稿 应用组件 已定稿 控制器(Controller) 已定稿 视图(View) 已定稿 模型(Model) 已定稿 过滤器 已定稿 小部件(Widget) 已定稿 模块(Module) 已定稿 前端资源(Asset) 已定稿 扩展(extensions) 请求处理 已定稿 运行概述 已定稿 引导(Bootstrapping) 已定稿 路由(Route)引导与创建 URL 已定稿 请求(Request) 已定稿 响应(Response) 已定稿 Sessions(会话)和 Cookies 已定稿 错误处理 已定稿 日志 关键概念 已定稿 组件(Component) 已定稿 属性(Property) 已定稿 事件(Event) 已定稿 行为(Behavior) 已定稿 配置(Configurations) 已定稿 类自动加载(Autoloading) 已定稿 别名(Alias) 已定稿 服务定位器(Service Locator) 已定稿 依赖注入容器(DI Container) 配合数据库工作 编撰中 数据访问对象(DAO) - 数据库连接、基本查询、事务和模式操作 编撰中 查询生成器(Query Builder) - 使用简单抽象层查询数据库 编撰中 活动记录(Active Record) - 活动记录对象关系映射(ORM),检索和操作记录、定义关联关系 编撰中 数据库迁移(Migration) - 在团体开发中对你的数据库使用版本控制 待定中 Sphinx 待定中 Redis 待定中 MongoDB 待定中 ElasticSearch 接收用户数据 编撰中 创建表单 已定稿 输入验证 编撰中 文件上传 待定中 多模型同时输入 显示数据 编撰中 格式化输出数据 待定中 分页(Pagination) 待定中 排序(Sorting) 编撰中 数据提供器 编撰中 数据小部件 编撰中 主题 安全 编撰中 认证(Authentication) 编撰中 授权(Authorization) 编撰中 处理密码 待定中 客户端认证 待定中 安全领域的最佳实践 缓存 已定稿 概述 已定稿 数据缓存 已定稿 片段缓存 已定稿 分页缓存 已定稿 HTTP 缓存 RESTful Web 服务 已定稿 快速入门 已定稿 资源 已定稿 路由 已定稿 格式化响应 已定稿 授权验证 已定稿 速率限制 已定稿 版本化 已定稿 错误处理 已定稿 测试
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值