2015-12-25 67 views
1

進出口新的Yii的,而不是肯定的是它可以從網址獲得動作和控制器的名稱,如:獲得來自URL操作和控制器的名稱,Yii2

/my_proj/backend/web/index.php?r=user%2Fcreate 

我需要讓他們在控制器(php)或客戶端(jQuery)。

我需要它,因爲,在點擊鏈接:

<?= Html::button(Yii::t('app', Yii::t('app','Create')), ['value' => Url::to(['create']),'class' => 'btn btn-success modalButton', 'id' => 'create-user']) ?> 

我想檢查當前用戶有權限create行動。我需要在客戶端,因爲我顯示在模式對話框中的創建視圖,如果用戶沒有權限,我不需要顯示彈出窗口。

$(".modalButton").click(function(){ 
    var permission = ?? //$(this)[0].id; //id works ok, but looking for another way 

    $.get('index.php?r=site/check-permission', {'permission': permission}, function(isAllowed){ 
     if(isAllowed == 1) 
     { 
      $(".modal").modal("show") 
         .find(".modalContent") 
         .load($(".modalButton").attr('value')); 
     } 
    }); 

    return false; 
}); 

我試圖用id的按鈕,並有指定的權限,但在尋找另一種解決方案。

回答

0

我覺得這是你所需要的 鑑於文件 -

$this->uniqueid - controller name 
$this->action->Id - action name. 

// ($this is CController instance) 

這是電流控制器

<?php echo $this->getUniqueId();?> 

OR

Yii::app()->controller->id 

OR

Yii::app()->getController()->getId() 

see this

OR

是的,你可以得到當前控制器/行動路線,通過反轉urlManager規則:

Yii::app()->urlManager->parseUrl(Yii::app()->request) 
+0

這得到電流控制器的動作名稱,但我有'用戶'模型的'index'視圖中的'創建'按鈕,並且我正在檢查另一個控制器'site'的權限 –

+0

你可以使用urlManager嗎? –

+0

它不工作,我得到服務器錯誤 –

相關問題