试图在非对象 Yii 1.1.15 上获取 属性
Trying to get property on a non object Yii 1.1.15
我尝试更改 CGridView 小部件的 Css 文件,在我的 config/main.php
中:
'components' => array(
'widgetFactory' => array(
'widgets' => array(
'CGridView' => array(
'cssFile' => Yii::app()->request->baseUrl . '/css/gridview.css',
),
),
),
...
我收到警告:
Trying to get property on a non object /path_to_project/protected/config/main.php on line 79
当我在视图文件中使用它时,我如何抑制这个警告,以及为什么会收到它。
P.S。是的,我可以将 display_errors
ini 设置为 false,消息将消失,但我想清楚地了解它。谢谢!
警告的原因是CHttpRequest对象Yii::app()->request
还没有被实例化。
来自 the API page for CApplication:
CApplication will undergo the following lifecycles when processing a
user request:
- load application configuration;
- set up error handling;
- load static application components;
- onBeginRequest: preprocess the user request;
- processRequest: process the user request;
- onEndRequest: postprocess the user request;
Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, the application will switch to its error handling logic and jump to step 6 afterwards.
你的错误发生在第一步。因此,Yii 的错误处理还没有设置好。唯一的选择是使用 @
operator:
抑制此警告
'cssFile' => @Yii::app()->request->baseUrl . '/css/gridview.css',
然而
这是个糟糕的主意,因为您实质上是在隐藏错误而不是修复它。
如果您的视图显示正确(没有 css 错误),您可以省略 Yii::app()->request->baseUrl
并只使用:
'cssFile' => '/css/gridview.css'
如果遇到错误,您可以在扩展 CWidgetFactory
的 components
文件夹中创建一个 class,并在此处设置依赖于其他组件的任何变量,例如
class MyWidgetFactory extends CWidgetFactory {
public function init() {
parent::init();
$this->widgets['CGridView']['cssFile'] = Yii::app()->request->baseUrl.'css/gridview.css';
}
}
您需要调整 components
才能使用此文件:
'components' => array(
'widgetFactory' => array(
'class' => 'MyWidgetFactory'
...
我尝试更改 CGridView 小部件的 Css 文件,在我的 config/main.php
中:
'components' => array(
'widgetFactory' => array(
'widgets' => array(
'CGridView' => array(
'cssFile' => Yii::app()->request->baseUrl . '/css/gridview.css',
),
),
),
...
我收到警告:
Trying to get property on a non object /path_to_project/protected/config/main.php on line 79
当我在视图文件中使用它时,我如何抑制这个警告,以及为什么会收到它。
P.S。是的,我可以将 display_errors
ini 设置为 false,消息将消失,但我想清楚地了解它。谢谢!
警告的原因是CHttpRequest对象Yii::app()->request
还没有被实例化。
来自 the API page for CApplication:
CApplication will undergo the following lifecycles when processing a user request:
- load application configuration;
- set up error handling;
- load static application components;
- onBeginRequest: preprocess the user request;
- processRequest: process the user request;
- onEndRequest: postprocess the user request;
Starting from lifecycle 3, if a PHP error or an uncaught exception occurs, the application will switch to its error handling logic and jump to step 6 afterwards.
你的错误发生在第一步。因此,Yii 的错误处理还没有设置好。唯一的选择是使用 @
operator:
'cssFile' => @Yii::app()->request->baseUrl . '/css/gridview.css',
然而
这是个糟糕的主意,因为您实质上是在隐藏错误而不是修复它。
如果您的视图显示正确(没有 css 错误),您可以省略 Yii::app()->request->baseUrl
并只使用:
'cssFile' => '/css/gridview.css'
如果遇到错误,您可以在扩展 CWidgetFactory
的 components
文件夹中创建一个 class,并在此处设置依赖于其他组件的任何变量,例如
class MyWidgetFactory extends CWidgetFactory {
public function init() {
parent::init();
$this->widgets['CGridView']['cssFile'] = Yii::app()->request->baseUrl.'css/gridview.css';
}
}
您需要调整 components
才能使用此文件:
'components' => array(
'widgetFactory' => array(
'class' => 'MyWidgetFactory'
...