php Yii框架中homeUrl和baseUrl的区别是什么?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/12494714/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
What is the difference between homeUrl and baseUrl in Yii framework?
提问by raghul
What is the difference between homeUrland baseUrlin Yii framework?
是什么区别homeUrl和的baseUrl在Yii框架?
采纳答案by Nimir
From the docs:
从文档:
baseUrl:
基本网址:
Returns the relative URL for the application. This is similar to scriptUrl except that it does not have the script file name, and the ending slashes are stripped off.
返回应用程序的相对 URL。这与 scriptUrl 类似,只是它没有脚本文件名,并且去掉了结尾斜线。
While, homeUrlis
而homeUrl是
the homepage URL
主页网址
Try echoing somewhere at your application to examine each by yourself:
尝试在您的应用程序中的某处回显以自己检查每个:
echo Yii::app()->getBaseUrl(true);// true tells to get a relative url, false the other way
echo Yii::app()->getHomeUrl();
How to use each?
各个怎么用?
baseUrlis as @bcmcfc said can be useful as a base for all links in your applications.
的baseUrl是@bcmcfc说可以为您的应用程序的所有链接的基础是有用的。
Now imagine you wanted to link to an image in web_root/myapp/img/if for example you did that using absolute path e.g <img src="C:/wwww/myapp/img/somepic.jpg>Let's say you finished all your development and now you want to deploy to some linuxserver!!
现在想象一下你想链接到一个图像,web_root/myapp/img/例如,如果你使用绝对路径这样做,例如<img src="C:/wwww/myapp/img/somepic.jpg>假设你完成了所有的开发,现在你想部署到某个linux服务器!
You can see that all your links will be broken :( but if instead you did: <img src= <?php Yii::app()->baseUrl() ?> . "/img/somepic.jpg"Everything should work fine :)
你可以看到你所有的链接都将被破坏:(但如果你这样做了:<img src= <?php Yii::app()->baseUrl() ?> . "/img/somepic.jpg"一切都应该正常工作:)
homeUrlis simply the landing page for your app. Well i didn't use this before but i guess you can set diffirent homeurls according to User role after login for example!!
homeUrl只是您的应用程序的登录页面。好吧,我之前没有使用过这个,但我想你可以在登录后根据用户角色设置不同的 homeurls !
回答by Einlanzer
Yii::app()->getBaseUrl(true); // => http://localhost/yii_projects
Yii::app()->getHomeUrl(); // => /yii_projects/index.php
Yii::app()->getBaseUrl(false); // => /yii_projects
This is the best practice in pointing directory of images etc. or the file itself rather than hardcoding the path which can change name"
这是指向图像等目录或文件本身的最佳实践,而不是对可以更改名称的路径进行硬编码”
回答by bcmcfc
One is the base URL for the links in your app. The other is the home URL. The home URL would be based on the base URL and would be a particular route or page, for example, that is redirected to after logging in.
一个是应用程序中链接的基本 URL。另一个是主页 URL。主页 URL 将基于基本 URL,并且可以是特定的路由或页面,例如,登录后重定向到的页面。

