YII 2 Get Site URL

无人久伴 提交于 2020-08-06 07:50:30

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!