简体   繁体   中英

Yii2 get controller/action from url

How in yii2 get controller/action from url. Attention!!! I ask about Yii2. There are answers that consern only Yii1 at this forum.

Added. I find smth like Yii::app()->getUrlManager()->parseUrl(' http://eewee.djn ')); but in yii2

Added. I get refferer url to anather conrtoller, where i want to parse it url by yii2 and get controller/action.

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 . The Request object needed because such construction:

list($controller, $actionID) =
    \Yii::$app->createController(parse_url(\Yii::$app->request->referrer, PHP_URL_PATH));

not works with parameters in 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 ?>

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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