php 在 Yii 中获取当前控制器和操作 ID
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19377000/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Get current controller and action id in Yii
提问by Hamid Ghorashi
I want to force all users to log in before accessing pages of my site. I have followed Larry Ullman's tutorial Forcing Login for All Pages in Yii.
我想强制所有用户在访问我网站的页面之前登录。我已经按照 Larry Ullman 的教程在 Yii 中强制登录所有页面。
According to the tutorial you can make an exception for some pages to avoid redirecting to the log in page. In order to check the current controller it has checked $_GET
value. My problem is that I have used urlManager
to rewrite the URL and $_GET
gives me a null value. Is there any method I can use to get the current controller and action in the score of my class?
根据教程,您可以对某些页面进行例外处理,以避免重定向到登录页面。为了检查当前控制器,它已检查$_GET
值。我的问题是我曾经urlManager
重写过 URL 并$_GET
给了我一个空值。有什么方法可以用来获取我班级分数中的当前控制器和动作?
I tried the following but it is not accessible in the scope of my component class:
我尝试了以下操作,但在我的组件类范围内无法访问它:
Yii::app()->controller->getId
回答by Korcholis
Did you try:
你试过了吗:
Yii::app()->controller->id
and:
和:
Yii::app()->controller->action->id
?
?
回答by eXtreme
Yes you can get the current controller/action
route, by reversing urlManager
rule:
是的,您可以controller/action
通过反转urlManager
规则获取当前路线:
Yii::app()->urlManager->parseUrl(Yii::app()->request)
回答by Kuldeep Dangi
As now in Yii2
现在在 Yii2 中
get current controller name
获取当前 controller name
Yii::$app->controller->id
current controller object
当前的 controller object
Yii::$app->controller
current action name
:
当前action name
:
Yii::$app->controller->action->id
current route
:
当前route
:
Yii::$app->requestedRoute
回答by Marco Moreno
Using Yii2, obtain the current controller object with:
使用 Yii2,获取当前控制器对象:
Yii::$app->controller
From the controller, obtain the current action as a string using:
从控制器中,使用以下命令以字符串形式获取当前操作:
Yii::$app->controller->action->id
回答by Jinsong Li
In Yii2:
在 Yii2 中:
The problem of calling Yii::$app->controller->id
is that when you call it somewhere (example: in one of your top-level abstract controller), Yii::$app->controller
might not be instantiated yet, so it will return error.
调用的问题Yii::$app->controller->id
在于,当您在某处调用它时(例如:在您的顶级抽象控制器之一中),Yii::$app->controller
可能尚未实例化,因此它会返回错误。
Just directly call urlManager
to map request to route:
只需直接调用urlManager
映射请求到路由:
var_dump(Yii::$app->urlManager->parseRequest(Yii::$app->request))
回答by CreatoR
Try Yii::app()->controller->getRoute()
尝试 Yii::app()->controller->getRoute()
回答by Sankalp Singha
If I get you question correctly, you are basically trying to stop access to certain actionsin the controller from being accessed without being logged in right?
如果我问你正确的问题,你基本上是在试图阻止访问控制器中的某些操作而无需登录,对吗?
If this is what you are after, the correct method to do it is this :
如果这是您所追求的,那么正确的方法是:
Make a
actionMethod()
in the controller like so :class SomeController extends CController{ public function actionSomeAction(){ ... More code... }
After that, you can access the site using : path/to/application/controllerName/actionName
- Now if you want to force the user to log in before accessing the action, do this :
做一个
actionMethod()
像这样的控制器:class SomeController extends CController{ public function actionSomeAction(){ ... More code... }
之后,您可以使用以下命令访问该站点:path/to/application/controllerName/actionName
- 现在,如果您想强制用户在访问操作之前登录,请执行以下操作:
Make an access control like so :
像这样进行访问控制:
/**
* @return array action filters
*/
public function filters()
{
return array(
'accessControl', // perform access control for CRUD operations
);
}
/**
* Specifies the access control rules.
* This method is used by the 'accessControl' filter.
* @return array access control rules
*/
public function accessRules()
{
return array(
array('allow', // allow authenticated user to perform 'create' and 'update' actions
'actions' => array('**yourActionMethodName**'),
'users' => array('@'),
),
array('deny', // deny all users
'users' => array('*'),
),
);
}
Now only authenticated users would be able to access the URL.
现在只有经过身份验证的用户才能访问该 URL。
I hope it solved your problem.
我希望它解决了你的问题。
If you simply want to check if the user is a guest and if he is, send him to the login page everytime:
If you simply want to check if the user is a guest and if he is, send him to the login page everytime:
In the config/main.php, add the following :
在 config/main.php 中,添加以下内容:
'defaultController' => 'controllerName/actionMethod',
And in that controller just add the above access rule. Now, by default you are opening the site to an access controlled method. So it would automatically redirect you to the login page.
在该控制器中只需添加上述访问规则。现在,默认情况下,您正在以访问控制方法打开站点。所以它会自动将您重定向到登录页面。
Even another method
:
Even another method
:
Just add this in the views/layouts/main.php
只需在views/layouts/main.php 中添加它
<?php
if(Yii::app()->user->isGuest)
{
$this->redirect('/site/login');
}
?>
回答by sergey
if (Yii::$app->requestedAction->id == "index") {
//do something
}