定义基本 url 常量而不是使用 base_url()?
不是为每个链接调用 function:
base_url()
,而是在 Constants.php: 中定义基本 url,然后使用该常量是否有意义
define('BASE_URL', 'http://mysite.com/');
,这样函数调用就不会在页面上多次进行?
Instead of calling the function:
base_url()
for every single link, would it make sense to define your base url in constants.php:
define('BASE_URL', 'http://mysite.com/');
and then use that constant so that the function call isn't potentially made many multiple times on a page?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
不完全是。如果您使用以下任何函数,您将遇到意外行为:
redirect()
site_url()
base_url()
form_open ()
anchor()
这不是一个详尽的列表——许多其他函数都依赖于基本 url 配置设置。更好的方法可能是使用相对链接,而不是硬编码绝对 URL。
我本人也曾尝试使用
site_url()
来定义每个链接,以此作为偏执的衡量标准(想知道 URL 结构是否会改变)。但实际上,对站点地图进行大规模修改会带来比链接更困难的问题。为了发展,我现在只在有意义的地方使用相对链接。干杯!
Not entirely. You'll run into unexpected behavior if you use any of the following functions:
redirect()
site_url()
base_url()
form_open()
anchor()
That's not an exhaustive list--many other functions depend on the base url config setting. A better approach might be to use relative links instead, rather than hard-coding absolute URLs.
I, myself, have been tempted to use
site_url()
to define every link as a measure of paranoia (wondering to myself whether the URL structure will ever change). But really, undergoing a massive overhaul of the sitemap will create more difficult problems than just the links. For the sake of development, I now just use relative links where it makes sense.Cheers!
在我看来,我不会担心多次调用
base_url()
- 我怀疑它会对性能产生重大影响。此外,我不喜欢使用常量的想法,因为基本 URL 本质上会在两个不同的地方定义两次。一次在
config.php
中,一次在BASE_URL
常量所在的位置。然而,这只是我的想法——也许其他人可以证明这一点。tl;dr 我不认为多次调用
base_url()
是一个问题,并且在大多数情况下,可能不会对性能产生重大影响。In my opinion, I wouldn't worry about multiple calls to
base_url()
- I doubt it will have a significant impact on performance.Additionally, I dislike the idea of using a constant because the base URL will essentially be defined twice, in two different places. Once in
config.php
and once wherever theBASE_URL
constant would live. However, that's just my thought on it - perhaps someone else could make the case for it.tl;dr I don't think multiple calls to
base_url()
is a problem and, in most cases, probably won't have a significant impact on performance.