YII 2 Get Site URL
Asked Answered
P

2

10

My Application is deployed on localhost/upload.

I am using following code to generate relative URL.

 Url::to('@web/my_controller/action'); // it returns /upload/my_controller/action

But, I need full URL like this instead: http://localhost/upload/my_controller/action.

Am I missing something?

Planter answered 15/1, 2015 at 8:30 Comment(0)
P
11

You should simply use a route :

Url::to(['my_controller/action']);

And if you want an absolute base url :

Url::to(['my_controller/action'], true);

Read more :

http://www.yiiframework.com/doc-2.0/yii-helpers-baseurl.html#to()-detail

http://www.yiiframework.com/doc-2.0/guide-runtime-routing.html#creating-urls

Prolong answered 15/1, 2015 at 8:49 Comment(2)
Nope, i know that but it only returns my_controller/actionPlanter
Finally i figured it out, we just need to pass true after the url eg: Url:to(['my_controller/action'],true) it will return example.com/my_controller/action.Planter
E
2

Follow urlManager And Request In Yiiframework 2.0 with example


  • Yii::$app->basePath ****>>>>**** D:\wamp\www\yiiframework2.0\project\backend

  • Yii::$app->homeUrl; ****>>>>**** /yiiframework2.0/project/backend/web/index.php

  • Yii::$app->getUrlManager()->createUrl(‘user’) ****>>>>**** /yiiframework2.0/project/backend/web/index.php?r=user

  • Yii::$app->urlManager->createUrl([‘site/page’, ‘id’ => ‘about’]) ****>>>>****
    /yiiframework2.0/project/backend/web/index.php?r=site%2Fpage&id=about

  • Yii::$app->urlManager->createUrl([‘site/view’, ‘id’ => 105]) ****>>>>**** /yiiframework2.0/project/backend/web/index.php?r=site%2Fview&id=105



  • Yii::$app->request->baseUrl ****>>>>**** /yiiframework2.0/project/backend/web


  • Yii::$app->request->url ****>>>>****
    /yiiframework2.0/project/backend/web/index.php
Eudemonia answered 7/3, 2017 at 7:7 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.