[英]Yii2 get controller/action from url
How in yii2 get controller/action from url. 在yii2中如何从网址获取控制器/操作。 Attention!!! 注意!!! I ask about Yii2. 我问关于Yii2。 There are answers that consern only Yii1 at this forum. 在此论坛上,仅回答Yii1的问题。
Added. 添加。 I find smth like Yii::app()->getUrlManager()->parseUrl(' http://eewee.djn ')); 我发现像Yii :: app()-> getUrlManager()-> parseUrl(' http://eewee.djn '))的东西; but in yii2 但是在yii2中
Added. 添加。 I get refferer url to anather conrtoller, where i want to parse it url by yii2 and get controller/action. 我得到另一个URL到anather conrtoller,在这里我想通过yii2解析URL并获取控制器/动作。
That worked for me 那对我有用
$request = new Request(['url' => parse_url(\Yii::$app->request->referrer, PHP_URL_PATH)]);
$url = \Yii::$app->urlManager->parseRequest($request);
list($controller, $actionID) = \Yii::$app->createController($url[0]);
You receive your action in $actionID
and controller name in the $controller->id
. 您会在$actionID
和$controller->id
中的控制器名称中收到操作。 The Request object needed because such construction: 需要Request对象,因为这样的构造:
list($controller, $actionID) =
\Yii::$app->createController(parse_url(\Yii::$app->request->referrer, PHP_URL_PATH));
not works with parameters in url. 不适用于url中的参数。
list($controller, $actionID) = Yii::$app->createController($url);
如果您需要作为对象采取行动,请尝试此
$action = $controller->createAction($actionID);
Try this! 尝试这个!
<?php echo Yii::$app->controller->id; // controller id ?>
<?php echo Yii::$app->controller->action->id; // controller action id ?>
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.