yii view

  上一篇主要了解了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

  1. /** 
  2.  * This is the default 'index' action that is invoked 
  3.  * when an action is not explicitly requested by users. 
  4.  */  
  5. public function actionIndex()  
  6. {  
  7.     $viewData=array();  
  8.       
  9.     // renders the view file 'protected/views/site/index.php'  
  10.     // using the default layout 'protected/views/layouts/main.php'  
  11.     $viewData['var1'] = '这是var1变量的对应的值';  
  12.     $this->render('index',$viewData);  
  13. }  
 
  1. <?php $this->pageTitle=Yii::app()->name; ?>  
  2.   
  3. <h1>Welcome to <i><?php echo CHtml::encode(Yii::app()->name); ?></i></h1>  
  4.   
  5. <p>Congratulations! You have successfully created your Yii application.</p>  
  6.   
  7. <p>You may change the content of this page by modifying the following two files:</p>  
  8. <ul>  
  9.     <li>View variable: <tt><?php echo $var1; ?></tt></li>  
  10.     <li>View file: <tt><?php echo __FILE__; ?></tt></li>  
  11.     <li>Layout file: <tt><?php echo $this->getLayoutFile('main'); ?></tt></li>  
  12. </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" />

<linkrel="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中都可以使用。这里要注意。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值