1

How to get the full URl in address bar to my Yii2 Model using yii\helpers\Url ?

I use $currentUrl = Yii::$app->request->url; but it return site/submit which is defined already in my UrlManager :

'urlManager'   => [
    'enableStrictParsing' => true,
    'enablePrettyUrl'     => true,
    'showScriptName'      => false,
    'rules'               => [
        '/'            => 'site/index',
        'site/submit'  => 'site/submit',
        'admin'        => 'admin/index',
        'admin/login'  => 'admin/login',
        'admin/index'  => 'admin/index',
        'admin/logout' => 'admin/logout',
        '/<url:.+>'    => 'site/index',
        'defaultRoute' => 'site/index',

    ],
],

Best regards,

Belicoff
  • 69
  • 1
  • 12
  • `// http://www.example.com/images/logo.gif echo Url::to('@web/images/logo.gif', true); ` Resource: https://www.yiiframework.com/doc/api/2.0/yii-helpers-baseurl#to()-detail – Serghei Leonenco Jul 16 '20 at 17:20
  • Also here is the similar answer: https://stackoverflow.com/questions/27959337/yii-2-get-site-url – Serghei Leonenco Jul 16 '20 at 17:23
  • They return only my https://example.com/controller/action !! if i want the full url https://example.com/controller/action/?campagneID=test&keyword=string ? I made some tests with Yii2 url helpers they return https://example.com/site/submit previously declared in UrlManager. – Belicoff Jul 16 '20 at 17:37
  • i use Yii2 thats not work with Yii::$app->request->createAbsoluteUrl( Yii::$app->request->getBaseUrl() ); – Belicoff Jul 16 '20 at 17:44
  • This should work like expected: `Yii::$app->request->absoluteUrl` Return `http://www.example.com/site/index?Model%5Bcategory%5D=22` – Serghei Leonenco Jul 16 '20 at 17:51
  • Sorry, but the return **https://www.example.com/site/submit** – Belicoff Jul 16 '20 at 17:56
  • I think the problem is in ** 'rules' => [ '/' => 'site/index', 'site/submit' => 'site/submit', 'admin' => 'admin/index', 'admin/login' => 'admin/login', 'admin/index' => 'admin/index', 'admin/logout' => 'admin/logout', '/' => 'site/index', 'defaultRoute' => 'site/index', ],* – Belicoff Jul 16 '20 at 17:57
  • Provide url what you trying to get – Serghei Leonenco Jul 16 '20 at 17:57
  • @SergheiLeonenco, thanks, my url is : ** https://www.example.com/?id=CAMP1&key=%7Bkeyword%7D&dev=%7Bdevice%7D ** it's represent a destination url coming from google – Belicoff Jul 16 '20 at 18:00
  • Real example : https://www.example.com/?id=CAMP1&key=meilleure%20offre%20internet&dev=m – Belicoff Jul 16 '20 at 18:01
  • It's solved with hack ==> $current_url = $_SERVER["HTTP_REFERER"]; thanks @SergheiLeonenco (y) – Belicoff Jul 17 '20 at 08:36

2 Answers2

2
Yii::$app->request->absoluteUrl

enter image description here

Plz follow this yii2 document

Golam Sorwar
  • 1,255
  • 15
  • 12
1

Yii::app()->createAbsoluteUrl(Yii::app()->request->url)

Ketan Vekariya
  • 334
  • 1
  • 11