CController::renderDynamic()导致页面无法打开!

我用的是Apache/2.2.14 (Win32) PHP/5.2.6 Yii Framework/1.1.0

修改了demos下示例blog的layouts里面的column2.php就出现问题了。


<?php if(!Yii::app()->user->isGuest) $this->renderDynamic($this->widget('UserMenu')); ?>

CController::renderDynamic()里面不能用CController::widget,那么怎么让$this->widget(‘UserMenu’)不被缓存?

注意查看renderDynamic的参数规范。你应该用:$this->renderDynamic(‘widget’, ‘UserMenu’);

看了一下API文档,按你说的用$this->renderDynamic(‘widget’, ‘UserMenu’);结果报错:Object of class UserMenu could not be converted to string

调试了一下,发现是因为CController::widget的返回值是CWidget类型的对象,看了API上写的也是。是不是这里不能用widget?Yii的demo blog里面UserMenu是从CPortlet继承的类。如果这里不能用widget,UserMenu类就是多余的了,要自己写个函数来返回UserMenu的html代码再在视图里用$this->renderDynamic()来调用了。还有好多其他的用widget的地方也都要改。这样写感觉结构好乱。有没有一个比较好的解决方案?

出错的call stack是怎样的?

PHP Error

描述

Object of class UserMenu could not be converted to string

源文件

D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CController.php(343)

00331:

00332: /**

00333: * Postprocesses the dynamic output.

00334: * This method is internally used. Do not call this method directly.

00335: * @param string output to be processed

00336: * @return string the processed output

00337: * @since 1.0.4

00338: */

00339: public function processDynamicOutput($output)

00340: {

00341: if($this->_dynamicOutput)

00342: {

00343: $output=preg_replace_callback(’/<###dynamic-(\d+)###>/’,array($this,‘replaceDynamicOutput’),$output);

00344: $this->_dynamicOutput=null;

00345: }

00346: return $output;

00347: }

00348:

00349: /**

00350: * Replaces the dynamic content placeholders with actual content.

00351: * This is a callback function used internally.

00352: * @param array matches

00353: * @return string the replacement

00354: * @see processOutput

00355: */

堆栈追踪

#0 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CController.php(343): preg_replace_callback()

#1 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\widgets\COutputCache.php(194): PostController->processDynamicOutput()

#2 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\widgets\COutputCache.php(148): COutputCache->run()

#3 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\filters\CFilterChain.php(126): COutputCache->filter()

#4 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\filters\CFilter.php(41): CFilterChain->run()

#5 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CController.php(983): CAccessControlFilter->filter()

#6 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\filters\CInlineFilter.php(59): PostController->filterAccessControl()

#7 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\filters\CFilterChain.php(126): CInlineFilter->filter()

#8 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CController.php(283): CFilterChain->run()

#9 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CController.php(257): PostController->runActionWithFilters()

#10 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CWebApplication.php(320): PostController->run()

#11 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\web\CWebApplication.php(120): CWebApplication->runController()

#12 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\yii\base\CApplication.php(135): CWebApplication->processRequest()

#13 D:\Program Files\Apache Software Foundation\Apache2.2\htdocs\myapp\index.php(11): CWebApplication->run()

2010-03-16 01:05:21 Apache/2.2.14 (Win32) PHP/5.2.6 Yii Framework/1.1.0

我刚给widget()函数添加了一个参数。你可以试试:$this->renderDynamic(‘widget’, ‘UserMenu’, array(), true);

好了,这下完美了。非常感谢强的帮忙和解决问题的及时。Yii很好很强大! :D

这个方式源代码里或api里貌似都没有说明,能不能加上呀。这个真的很完美^_^

顺便再问一下

假如我需要对“新闻详细内容页”进行页面缓存,但是我又想记录每次访问的点击量,请问如何是好?

正常情况下,action是这样的

function actionView() {

$model=$this->loadModel();

$model->incHit();//增加点击量

$this->render(…)

}

:angry: