I’m not an old hand when it comes to PHP, and certainly not one when it comes to Yii, so this could go either way.
In declaring a CGridView I’m trying to use an inline function with RBAC to control the ‘template’ property. I’ve got this view code that works:
<?php
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'selectableRows' => '0',
'columns' => array(
array(
'name' => 'user_email',
'header' => 'User Email',
'value' => 'CHtml::encode($data->user_email)',
'type' => 'raw'
),
array( // display a column with "view", "update" and "delete" buttons
'class' => 'CButtonColumn',
'template' => temp()
)
)
));
function temp()
{
if (Yii::app()->user->checkAccess(User::ROLE_ADMIN))
return '{view}{update}{delete}';
else
return '{view}';
}
?>
but I’d rather say this for ‘template’:
$this->widget('zii.widgets.grid.CGridView', array(
'dataProvider' => $dataProvider,
'selectableRows' => '0',
'columns' => array(
array(
'name' => 'user_email',
'header' => 'User Email',
'value' => 'CHtml::encode($data->user_email)',
'type' => 'raw'
),
array( // display a column with "view", "update" and "remove" buttons
'class' => 'CButtonColumn',
'template' => function ()
{
if (Yii::app()->user->checkAccess(User::ROLE_ADMIN))
return '{view}{update}{delete}';
else
return '{view}';
}
)
)
));
Unfortunately this throws an exception:
CBaseController->widget("zii.widgets.grid.CGridView", array("dataProvider" => CActiveDataProvider, "selectableRows" => "0", "columns" => array(array("name" => "user_email", "header" => "User Email", "value" => "CHtml::encode($data->user_email)", "type" => "raw"), array("class" => "CButtonColumn", "template" => Closure))))
I don’t care about whether or not this is the right place to make the decision about what the template should be, I just want to know what it is about the inline declaration (“Closure” class?) that’s a problem for PHP/Yii (PHP, probably).
TIA.