简体   繁体   English

从网址,Yii2获取操作和控制器名称

[英]Get action and controller name from Url, Yii2

Im new to Yii, and not sure is it possible to get action and controller names from url like: 我是Yii的新手,不确定是否可以从url中获取动作和控制器名称,例如:

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

I need to get them in controller( php ) or client side ( jQuery ). 我需要在controller( php )或客户端( jQuery )中获取它们。

I need it because, on button click: 我需要它,因为在按钮上单击:

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

I want to check if the current user has permissions for create action. 我想检查当前用户是否具有create操作的权限。 I need this on client side, because I show create view in modal dialog, and if user does not have permission, i don't need to show popup. 我在客户端需要它,因为我在模式对话框中显示创建视图,并且如果用户没有权限,则不需要显示弹出窗口。

  $(".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;   
});

I tried to use id for button, and specify permission there, but looking for another solution. 我尝试将id用于按钮,并在那里指定权限,但是正在寻找另一种解决方案。

i think this is what you need In view file - 我认为这是您需要的在查看文件中-

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

// ($this is CController instance) 

This is for the current controller 这是当前的控制器

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

OR 要么

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

OR 要么

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

see this 看到这个

OR 要么

Yes you can get the current controller/action route, by reversing urlManager rule: 是的,您可以通过反转urlManager规则来获取当前的控制器/操作路线:

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM