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);
    }
相關文章
相關標籤/搜索