Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Yii2 parse action name from referer url

Tags:

yii2

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

like image 926
J S Avatar asked Dec 14 '22 04:12

J S


1 Answers

You can easily parse the URL by mocking a Request object and passing it to the UrlManger.

Imagine we have URL http://example.com/user/42, and UrlManager has 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, isn't it? :)

like image 91
SilverFire Avatar answered Feb 03 '23 11:02

SilverFire