YII Framework学习教程-YII的V-view的深入了解和使用-2011-11-16

  上一篇主要了解了Controller如何调用相应的view视图文件和Controller如何推送数据到view试图文件中。这里在详细的讲讲,如何更全面的使用view。

  其实YII中的view并不是一个理想的view。理想的view可能只是html代码。不会涉及到php代码。。但是又有那个框架真正的达到这种完美的分离呢。

这里把view中常用的方法罗列一下。以便记忆,后期可以灵活使用

  打开文件

 /testwebap/protected/views/site/index.php

 /yii_dev/testwebap/protected/controllers/SiteController.php

1.PHP常用的方法在这里都是可以是的例如date(),string相关函数,数字相关函数,__FILE__等等诸多。

2.从controller的action中传递普通变量到view

	/**
	 * This is the default 'index' action that is invoked
	 * when an action is not explicitly requested by users.
	 */
	public function actionIndex()
	{
		$viewData=array();
		
		// renders the view file 'protected/views/site/index.php'
		// using the default layout 'protected/views/layouts/main.php'
		$viewData['var1'] = '这是var1变量的对应的值';
		$this->render('index',$viewData);
	}
 
<?php $this->pageTitle=Yii::app()->name; ?>

<h1>Welcome to <i><?php echo CHtml::encode(Yii::app()->name); ?></i></h1>

<p>Congratulations! You have successfully created your Yii application.</p>

<p>You may change the content of this page by modifying the following two files:</p>
<ul>
	<li>View variable: <tt><?php echo $var1; ?></tt></li>
	<li>View file: <tt><?php echo __FILE__; ?></tt></li>
	<li>Layout file: <tt><?php echo $this->getLayoutFile('main'); ?></tt></li>
</ul>



3. Yii::app()->name

是yii_dev/testwebap/protected/config/main.php中'name'的值

一次类推,如果想用,可以随便使用配置文件中的其他变量,

4.Yii::app()->request->baseUrl

如果url是www.localyii.com/testwebap/index.php?r=site/index

那么Yii::app()->request->baseUrl就是 /testwebap

例如

<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="/testwebap/css/screen.css" media="screen, projection" />


5.<?php echo CHtml::encode(Yii::app()->name); ?>

在代码中随处可见。打开代码,其实质就是

return htmlspecialchars($text,ENT_QUOTES,Yii::app()->charset);

应该不用多讲了吧,为什么是用,有什么好处,不言而喻。

6.Yii::powered();

是框架定义的。功能就是输出

return 'Powered by <a href="http://www.yiiframework.com/" rel="external">Yii Framework</a>.';

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17

18.

19.

20.

21.


这里把action中常用的方法罗列一下。以便记忆,后期可以灵活使用

1.php中的$_GET,$_POST可以正常使用

2.Yii::app()->homeUrl

如果url是http://www.localyii.com/testwebap/index.php?r=site/index

代表的是

/testwebap/index.php

3. throw new CHttpException(404,'The requested page does not exist.');

可以抛出异常。

4Yii::app()->end();

代表请求,处理结束。一般就是exit

5.Yii::app()->request->isPostRequest

判断是否提交表单

6.Yii::app()->request->isAjaxRequest

判断请求的类型

7.

8.

9.

10.

11.

12.

13.

14.

15.

16.

17

18.

19.

20.

21.

后期慢慢补充。


有的静态变量和方法可以在action和view中都可以使用。这里要注意。



评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值