gpt4 book ai didi

php - CDN 上的静态内容使用 PHP 获取地址

转载 作者:行者123 更新时间:2023-12-04 06:41:38 29 4
gpt4 key购买 nike

我对 PHP 编程很陌生,但我认为我会从一开始就这样做,所以我遇到了这个很好的 pdf Web Performance Boot Camp他建议:

All sites should always prepare for CDNized static content



这是如何:
<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/静态图像/等)?感谢您阅读本文。

最佳答案

如果您希望将来使用 CDN,这不是一个愚蠢的想法。

一个简单的函数如下所示:

 function getURL($url)  // Name it whatever you want
{
// Choose one of the following:
return $url; // If you're local
return "http://s.company.net/".$url; // If you're on a CDN or static server

}

和标记:
<link type="text/css" rel="stylesheet" href="<?php echo getURL("c/".$this->css_file) ?>" />

Do I have to put... at the beginning of every html/php file I create (that uses scripts/css/static images/etc.)?



是的。为将来可能需要引入的共享 PHP 设置包含某种中央 bootstrap 文件(一些框架称之为 bootstrap.php)可能是明智的。然后,该引导文件将依次包含 cdn.php .

关于php - CDN 上的静态内容使用 PHP 获取地址,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/4163321/

29 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com