动态库的静态加载和动态加载_以动态方式加载静态内容

动态库的静态加载和动态加载

This post was authored by Jeremy Martin. To learn more about Jeremy, click here.

这篇文章由杰里米·马丁 ( Jeremy Martin )发表。 要了解有关杰里米的更多信息, 请单击此处

One of the catch 22's of owning a blog or site on the cheap is that of not breaking the budget, while tip-toeing around an overly constrictive space/bandwidth quota. Along with countless others, I myself am often hanging in the aforementioned balance. One solution I recently deployed to stretch a little more mileage out of my current hosting package was to serve all my static content via one of the many dirt cheap or even free file hosting services out there.

以便宜的价格拥有博客或网站的陷阱22之一是不超出预算,同时脚步围绕过于狭窄的空间/带宽配额。 与无数其他人一样,我本人也经常处于上述平衡中。 我最近部署的一种解决方案,可以从当前的托管软件包中获取更多的收益,它可以通过许多廉价甚至免费的文件托管服务之一来提供我所有的静态内容。

Serving my static content from a remote location has had several benefits, such as reduced bandwidth consumption and fewer content requests. However, I immediately became aware of an inconvenience to this solution. Modifying any static content on my local machine (which I use for development and testing) forces me to change their references to local paths. The same is also true if I want to do any development while disconnected from the internet. And of course, these references must be switched back to point at my static hosting before deployment. I found this to be getting so tedious that I was simply testing in production - bad idea.

从远程位置提供我的静态内容有很多好处,例如减少带宽消耗和减少内容请求。 但是,我立即意识到此解决方案带来的不便。 修改本地计算机上的任何静态内容(用于开发和测试)会迫使我将其引用更改为本地路径。 如果我想在断开互联网连接的情况下进行任何开发,也是如此。 当然,在部署之前,必须将这些引用切换回指向我的静态主机。 我发现这变得非常乏味,以至于我只是在生产中进行测试-这是一个坏主意。

Well that's when I thought of this painfully obvious solution - while I'm quite sure it's not original, it is both very simple and very useful. I use a common.php file to hold all of my site-wide helper methods. I added to it this simple function:

嗯,那时候我想到了这个痛苦的显而易见的解决方案-虽然我很确定它不是原创的,但是它既非常简单又非常有用。 我使用common.php文件保存所有站点范围内的帮助器方法。 我添加了这个简单的功能:

function get_static_root() {
if($_SERVER['HTTP_HOST'] == 'localhost') {
return 'http://localhost/static';
} else {
return 'http://www.xxxxxx.com/static';
}
}

Then I organized all of my static content into the following directory structure:

然后,我将所有静态内容组织到以下目录结构中:

  • / (Site Root)

    /(站点根目录)

      • css

        CSS
      • js

        js
      • images

        图片

This causes all of my local static content to match the file structure of my remotely hosted static content. Now, when I want to reference an image, for example, I simply set the url equal to /images/image.jpg. If I'm testing on my local machine, the url will resolve to http://localhost/static/images/image.jpg, whereas in production it will resolve to http://www.xxxxxx.com/static/images/image.jpg.

这将导致我的所有本地静态内容都与远程托管的静态内容的文件结构相匹配。 现在,例如,当我要引用图像时,只需将URL设置为等于/images/image.jpg。 如果我在本地计算机上进行测试,则该URL将解析为http://localhost/static/images/image.jpg,而在生产环境中,它将解析为http://www.xxxxxx.com/static/images/ image.jpg。

While this does require a little more work up front, it has saved me a lot of time and makes development so much easier.

尽管这确实需要做更多的工作,但它节省了我很多时间,并使开发变得如此容易。

For those who like to visualize...

对于那些喜欢可视化的人...

Static Root Description

Well that's it! Many thanks to David for letting me guest author on his excellent blog!

就是这样! 非常感谢David让我在他的优秀博客上担任特邀作者!

关于杰里米·马丁 (About Jeremy Martin)

Jeremy Martin, born and raised outside of Seattle, Washington, received a B.S. in Computer Science in 2007. In July of the same year, he married his beautiful wife, Annie, and they now reside in the far, far away land of South Carolina. Jeremy is currently a software engineer for Charleston based Benefitfocus, and enjoys hanging out with his better half, and keeping his blog up to date (when she'll let him...).

杰里米·马丁(Jeremy Martin)在华盛顿州西雅图市外出生和长大,于2007年获得计算机科学学士学位。同年7月,他与美丽的妻子安妮(Annie)结婚,他们现在居住在遥远的南卡罗来纳州。 Jeremy目前是位于Charleston的Benefitfocus的软件工程师,并且喜欢与自己的另一半闲逛,并保持他的博客最新(当她允许他...时)。

翻译自: https://davidwalsh.name/load-static-content-dynamic

动态库的静态加载和动态加载

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值