Yii2 parsing action name from referent URL

I want to check the condition on the previous action name. I got the previous action url from Yii::$app->request->referrer. Now I want to parse only the name of the action. Or is there another way to directly get the action name for the referent.

+4
source share
1 answer

You can easily parse the URL by making fun of the object Requestand passing it to UrlManger.

Imagine that we have a URL http://example.com/user/42, but UrlManagerhas the following rule:['user/<id:\d+>' => 'user/view']

$request = new Request(['url' => parse_url(Yii::$app->request->referrer, PHP_URL_PATH)]);
$url = Yii::$app->urlManager->parseRequest($request);
var_dump($url); // ['user/view', 'id' => 42]

Cool, right? :)

+7
source

Source: https://habr.com/ru/post/1652438/


All Articles