活动视图中按钮的可见性

时间:2016-02-24 09:14:38

标签: yii2

他是我的view.php代码

<?php

use yii\helpers\Html;
use yii\widgets\DetailView;

/* @var $this yii\web\View */
/* @var $model app\models\CasualLeaves */
$this->title = $model->id;
$this->params['breadcrumbs'][] = ['label' => 'Casual Leaves', 'url' => ['index']];
$this->params['breadcrumbs'][] = $this->title;
?>
<div class="casual-leaves-view">

<h1><?= Html::encode($this->title) ?></h1>

<p>
    <?= Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Delete', ['delete', 'id' => $model->id], [
        'class' => 'btn btn-danger',
        'data' => [
            'confirm' => 'Are you sure you want to delete this item?',
            'method' => 'post',
        ],
    ]) ?>
    <?= Html::a(' Recommended', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
    <?= Html::a('Not Recommended', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']) ?>
</p>


<?= DetailView::widget([
    'model' => $model,
    'attributes' => [
    //    'id',
        'name_of_applicant',
        'designation',
        'start_date',
        'end_date',
        'total_number_of_days',
        'reason_for_leave',
        'no_of_leave_already_taken',
        'date_of_request',
      //  'recommendation',
      //  'status',
    ],
]) ?>

我有休假申请表。

申请人创建假期并保存。

点击“创建”按钮后,申请人只会看到更新按钮,那时我想要隐藏删除按钮。

但管理员在登录时查看删除按钮并检查应用程序

1 个答案:

答案 0 :(得分:1)

只有当用户是管理员时才可以播下删除按钮(在示例测试中RBAC可以('admin')权限)

<p>
<?php 

   echo Html::a('Update', ['update', 'id' => $model->id], ['class' => 'btn btn-primary']);
   if ( Yii::$app->User->can('admin') ){
     echo Html::a('Delete', ['delete', 'id' => $model->id], [
         'class' => 'btn btn-danger',
         'data' => [
             'confirm' => 'Are you sure you want to delete this item?',
             'method' => 'post',
         ],
    ]);
 }
echo Html::a('Recommended', ['update', 'id' => $model->id], 
         ['class' => 'btn btn-primary']);
echo Html::a('Not Recommended', ['update', 'id' => $model->id], 
         ['class' => 'btn btn-primary']);
?>
</p>