Yii::app()->user->isAdmin() 在布局页面中无法正常工作
在我的布局页面 Cmenu 可见 fn Yii::app()->user->isAdmin()
在我使用 Yii::app()- 时无法正常工作>user->isAdmin()
在其他视图中它显示正确的值但在布局中不起作用.我在 protected/views/layouts/main.php 中的代码
in my layout page Cmenu visible fn Yii::app()->user->isAdmin()
is not working properly when i use Yii::app()->user->isAdmin()
in someother view it showing correct value but not working in layout.
my code in protected/views/layouts/main.php
<?php $this->widget('zii.widgets.CMenu',array(
'items'=>array(
array('label'=>'Home', 'url'=>array('/site/index'),/*'visible'=>!Yii::app()->user->isGuest*/),
array('label'=>'Master','url'=>array('/site/master'),'visible'=>Yii::app()->user->isAdmin()),
array('label'=>'Transaction','url'=>array('/site/transaction'),'visible'=>Yii::app()->user->isAdmin()),
array('label'=>' Alotted Task','url'=>array('/site/alottedtask'),'visible'=>!Yii::app()->user->isGuest),
array('label'=>'Completed Task','url'=>array('/site/completedtask'),'visible'=>!Yii::app()->user->isGuest),
array('label'=>'Status Update', 'url'=>array('/site/statusupdate'),'visible'=>Yii::app()->user->isAdmin()),
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)
),
)); ?>
它显示了类似的错误
试图获取非对象的属性
更新:
我的错误页面http://localhost/tracktest/index.php
所以这是抛出非对象"错误的代码:
So this is the code that is throwing the "non-object" error:
return intval($user->role) == 1;
这意味着当 PHP 试图获取 $user
对象的 role
属性时,$user
实际上并不是一个对象.查看您的代码,这意味着 loadUser()
无法正常工作,或者 Yii::app()->user->id
未返回用户 ID.
This means that when PHP is trying to get the role
attribute of the $user
object, $user
is not actually an object. Looking at your code, this means that either loadUser()
is not working correctly, or Yii::app()->user->id
is not returning the user ID.
为了测试这一点,我会将其添加到您的函数中,以便打印出一些测试变量:
To test this, I would add this to your function so some test variables will be printed out:
function isAdmin() { // this should say "public function", btw
$testId = Yii::app()->user->id;
echo $testId;
print_r(User::model()->findByPk($testId));
die();
}
这应该让您查看是否正在获取用户 ID,以及用户是否正确加载.
This should let you see if you are getting the user ID, and if the user if being loaded properly.
祝你好运!