问题
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?
回答1:
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
回答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->urlManager->createAbsoluteUrl(”) ****>>>>****
http://127.0.0.1/yiiframework2.0/project/backend/web/index.php?r=
- Yii::$app->urlManager->createAbsoluteUrl(‘site/view/index’) ****>>>>**** http://127.0.0.1/yiiframework2.0/project/backend/web/index.php?r=site%2Fview%2Findex
- Yii::$app->request->baseUrl ****>>>>**** /yiiframework2.0/project/backend/web
- Yii::$app->request->absoluteUrl ****>>>>****
http://127.0.0.1/yiiframework2.0/project/backend/web/index.php
- Yii::$app->request->url ****>>>>****
/yiiframework2.0/project/backend/web/index.php
来源:https://stackoverflow.com/questions/27959337/yii-2-get-site-url