yii2 无权限的时候,不显示对应按钮方法

概要app

主要解决当咱们不给用户角色时, 对应的按钮就不作显示,而不是直接在视图中去注释代码。url

1:须要在Html::a方法中直接加入如下代码code

public static function a($text, $url = null, $options = [])
    { 
        if ($url !== null) {
            $options['href'] = Url::to($url);
            if (strstr($options['href'], '?'))
            {
                $newurl = substr($options['href'],0,strrpos($options['href'],'?'));
            }
            else {
                $newurl = $options['href'];
            }
           if (!\Yii::$app->user->can($newurl))
           {
               return false;
           }
            
        }
        return static::tag('a', $text, $options);
    }
相关文章
相关标签/搜索