使用 PHP 获取地址的 CDN 上的静态内容
我对 PHP 编程相当陌生,但我认为我应该从一开始就做,所以我遇到了这个精美的 pdf Web 性能训练营 他建议:
所有网站都应始终为 CDN 化静态内容做好准备
,这是如何做到的:
<img src=”<?php echo CDN(‘/i/left-menu-background.gif’) ?>”
等等,他还举了一个CDN功能如何的例子?应该看起来像:
sub CDN { return @_[1]; }
或者(当你最终在 CDN 上获得静态内容时)
sub CDN { return ‘http://s.company.net’ . @_[1]; }
(但这不是有效的 php,对吧?它看起来更像是 perl...)
无论如何,这将继续如何重写标头,例如:
<link type="text/css" rel="stylesheet" href="<?php echo $this->CDN("c/".$this->css_file) ?>" />
但老实说,我不知道该怎么做才是正确的。所以,我的问题是,如何为 CDN 准备我的 (php) 网站?子CDN功能放在哪里?它在有效的 php 中应该是什么样子?我如何/在哪里包含它?我是否必须
<?php require('../cdn.php'); ?>
在创建的每个 html/php 文件(使用脚本/css/静态图像/等)的开头添加 a ?感谢您阅读本文。
I'm rather new to PHP programming but I thought I'd do it right from the beginning, so I came across this fine pdf Web Performance Boot Camp where he suggests:
All sites should always prepare for CDNized static content
and this is how:
<img src=”<?php echo CDN(‘/i/left-menu-background.gif’) ?>”
etc., he also gave an example of how the CDN function? should look like:
sub CDN { return @_[1]; }
or (when you finally have your static content on a CDN)
sub CDN { return ‘http://s.company.net’ . @_[1]; }
(but that's not valid php, right? it looks more like perl...)
Anyway, this goes on with how to rewrite the header like:
<link type="text/css" rel="stylesheet" href="<?php echo $this->CDN("c/".$this->css_file) ?>" />
But honestly, I have no idea how to do it right. So, my question is, how to I prepare my (php) site for a CDN? Where do I put the sub CDN function? How should it look in valid php? How/Where do I include it? Do I have to put a
<?php require('../cdn.php'); ?>
at the beginning of every html/php file I create (that uses scripts/css/static images/etc.)? Thanks for reading this.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
如果您希望将来使用 CDN,这不是一个愚蠢的想法。
一个简单的函数如下所示:
和标记:
是的。为将来可能需要引入的共享 PHP 设置包含某种中央引导文件(某些框架称为
bootstrap.php
)可能是明智之举。然后,该引导文件将依次包含cdn.php
。If you're expecting to use a CDN in the future, this is not a stupid idea.
A simple function would look like this:
and the markup:
Yes. It might be wise to include some sort of central bootstrap file (some frameworks call it
bootstrap.php
) for future shared PHP settings that you may need to introduce. That bootstrap file would then, in turn, include thecdn.php
.