目标3——框架工作流程
2011-12-09 09:13
351 查看
1.Yii应用的静态结构
2.Yii应用的典型流程
1. 用户访问 http://www.example.com/index.php?r=post/show&id=1,Web 服务器执行入口脚本index.php 来处理该请求。
2. 入口脚本建立一个应用实例并运行之。
3. 应用从一个叫 request 的应用组件获得详细的用户请求信息。
4. 在名为 urlManager 的应用组件的帮助下,应用确定用户要请求的控制器和动作。
5. 应用建立一个被请求的控制器实例来进一步处理用户请求,控制器确定由它的actionShow 方法来处理show 动作。然后它建立并应用和该动作相关的过滤器(比如访问控制和性能测试的准备工作),如果过滤器允许的话,动作被执行。
6. 动作从数据库读取一个 ID 为 1 的 Post模型。
7. 动作使用 Post 模型来渲染一个叫 show 的视图,视图一般包含内容视图和布局视图,Yii一般读取内容视图的内容到$content的变量中,在布局视图中使用这个变量绘制完整个视图文件。
8. 视图读取 Post 模型的属性并显示之。
9. 视图运行一些挂件。
10. 视图的渲染结果嵌在布局中。
11. 动作结束视图渲染并显示结果给用户。
【1~4】入口文件:WebApp/index.php
1 <?php2
3 // change the following paths if necessary
4 $yii=dirname(__FILE__).'/../framework/yii.php';
//Yii框架框架位置
5 $config=dirname(__FILE__).'/protected/config/main.php';//Yii应用实例所需的配置文件
6
7 // remove the following lines when in production mode
8 defined('YII_DEBUG') or define('YII_DEBUG',true);
9 // specify how many levels of call stack should be shown in each log message
10 defined('YII_TRACE_LEVEL') or define('YII_TRACE_LEVEL',3);
11
12 require_once($yii); //包含Yii框架
13 Yii::createWebApplication($config)->run(); //创建应用实例
【5】控制器
5.1控制器类
protected/components/Controller.php是所有控制器的基类
5.2默认控制的设置framework/web/CWebApplication.php,CWebApplication::defaultController定义(
'site'
),
可以在配置文件修改
/**
63 * @return string the route of the default controller, action or module. Defaults to 'site'.
64 */
65 public $defaultController='site';
66 /**
67 * @var mixed the application-wide layout. Defaults to 'main' (relative to {@link getLayoutPath layoutPath}).
68 * If this is false, then no layout will be used.
69 */
动作类 默认动作在CController::
$defaultAction
定义(
'index'
),可以在CController的继承类重新定义
5.3修改默认控制器protected/config/main.php
'modules'=>array(
22 // uncomment the following to enable the Gii tool
23 /*
24 'gii'=>array(
25 'class'=>'system.gii.GiiModule',
26 'password'=>'Enter Your Password Here',
27 // If removed, Gii defaults to localhost only. Edit carefully to taste.
28 'ipFilters'=>array('127.0.0.1','::1'),
29 ),
30 */
31 ),
32
33 'defaultController'=>'post', //设置默认控制器类,一般在首页时使用,对应的文件为:PostController.php
34
35 // application components
36 'components'=>array(
37 'user'=>array(
38 // enable cookie-based authentication
39 'allowAutoLogin'=>true,
6.模型
7.视图
视图是一个包含了主要的用户交互元素的PHP 脚本.视图有一个名字,当渲染(render)时,名字会被用于识别视图脚本文件。视图的名称与其视图脚本名称是一样的。例如:
视图edit 的名称出自一个名为edit.php 的脚本文件。要渲染时,需通过传递视图的名称调用CController::render()。
这个方法将在“protected/views/控制器ID”目录下寻找对应的视图文件。
在视图脚本内部,我们可以通过$this 来访问控制器实例。我们可以在视图里以“$this->属性名”的方式获取控制器的任
何属性。
我们也可以用以下推送的方式传递数据到视图里:
$this->render('edit', array(
'var1'=>$value1,
'var2'=>$value2,
));
在以上的方式中, render() 方法将提取数组的第二个参数到变量里。其产生的结果是,在视图脚本里,我们可以直接访问变
量$var1 和$var2。
7.1布局
布局是一种用来修饰视图的特殊的视图文件。它通常包含了用户界面中通用的一部分视图。例如:布局可以包含header和footer 的部分,然后把内容嵌入其间。
......header here......
<?php echo $content; ?>
......footer here......
其中的$content 则储存了内容视图的渲染结果。
当使用render()时,布局被隐式应用。视图脚本protected/views/layouts/main.php 是默认的布局文件。这可以通过改变
CWebApplication::layout 进行自定义(public $layout='main';)。要渲染一个不带布局的视图,则需调用renderPartial() 。
7.1.1protected/views/layouts/main.php内容
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"><html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="language" content="en" />
<!-- blueprint CSS framework -->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/screen.css" media="screen, projection" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/print.css" media="print" />
<!--[if lt IE 8]>
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/ie.css" media="screen, projection" />
<![endif]-->
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/main.css" />
<link rel="stylesheet" type="text/css" href="<?php echo Yii::app()->request->baseUrl; ?>/css/form.css" />
<title><?php echo CHtml::encode($this->pageTitle); ?></title>
</head>
<body>
<div class="container" id="page">
<div id="header">
<div id="logo"><?php echo CHtml::encode(Yii::app()->name); ?></div>
</div><!-- header -->
<div id="mainmenu">
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index')),
array('label'=>'About', 'url'=>array('/site/page', 'view'=>'about')),
array('label'=>'Contact', 'url'=>array('/site/contact')),
array('label'=>'Login', 'url'=>array('/site/login'), 'visible'=>Yii::app()->user->isGuest),
array('label'=>'Logout ('.Yii::app()->user->name.')', 'url'=>array('/site/logout'), 'visible'=>!Yii::app()->user->isGuest)
),
)); ?>
</div><!-- mainmenu -->
<?php if(isset($this->breadcrumbs)):?>
<?php $this->widget('zii.widgets.CBreadcrumbs', array(
'links'=>$this->breadcrumbs,
)); ?><!-- breadcrumbs -->
<?php endif?>
<?php echo $content; ?>
<div id="footer">
Copyright © <?php echo date('Y'); ?> by My Company.<br/>
All Rights Reserved.<br/>
<?php echo Yii::powered(); ?>
</div><!-- footer -->
</div><!-- page -->
</body>
</html>
7.1.2修改默认的布局文件
'modules'=>array(// uncomment the following to enable the Gii tool
/*
'gii'=>array(
'class'=>'system.gii.GiiModule',
'password'=>'Enter Your Password Here',
// If removed, Gii defaults to localhost only. Edit carefully to taste.
'ipFilters'=>array('127.0.0.1','::1'),
),
*/
),
//'defaultController'=>'post', //设置默认控制器类,一般在首页时使用,对应的文件为:protected/controllers/PostController.php�
//'layout'=>'newlayout', //设置默认的布局文件,对应的文件为:protected/views/layouts/newlayout.php
7.1.3修改默认的布局文件2
另外一个需要指定布局的地方是设置Controller(控制器)类$layout属性。这样就允许针对不同Contorller(控制器)以更细的粒度进行设置。这就是在建立最初程序时定制的方法。当使用yiic工具来建立最初应用程序时,基于Webroot/protected/components/Controller.php来自动创建一个Controller(控制器)。打开该文件你将看到$layout属性被设置为”column1”。在Controller(控制器)级对布局文件的设置将覆盖CWebApplication类中的设置。让我们动手验证一下。新建一个叫newlayout.php的文件,并将其放入默认布局文件位置:/protected/views/layouts/。将上面的HTML添加进去后保存。下面我们将通过修改我们的站点controller(控制器)来使用这个新布局。打开SiteController.php然后重载layout属性设置,添加如下代码:
class
SiteController extends
Controller {
public$layout='newlayout';
这样只有这一个controller(控制器)会使用newlayout.php。至此,每一次基于SiteController调用render()方法都会使用newlayout.php布局文件。
相关文章推荐
- 1. Scrapy框架组件及工作流程
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件介绍
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件WinForm业务平台
- 小结:实例解析DAO设计模式工作流程(无框架)
- android telephony 工作流程(二)--UICC上层实现框架
- Scrapy框架的工作流程
- Yarn大体框架和工作流程研究
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件WinForm业务平台
- nodejs的Express框架源码分析、工作流程分析
- OSWorkflow : Java的工作流程框架介绍
- ffmpeg工作流程框架
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件WinForm业务平台
- Activiti5工作流程框架
- springMVC框架及基本工作流程
- RDIFramework.NET ━ .NET快速信息化系统开发框架 ━ 工作流程组件Web业务平台
- 搜索引擎—通用爬虫框架及工作流程
- Struts2 框架工作处理流程
- zhphp framework (三) 框架工作流程
- 使用otter实现MySQL跨机房双向同步 工作流程 环境 目标 名词解释 安装 配置 配置mysql 导入 manager 系统表 导入 node 同步算法依赖的系统表 管理配置 添加zookee
- 框架工作流程