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

发布于 2024-10-02 06:12:47 字数 972 浏览 3 评论 0原文

我对 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

感情旳空白 2024-10-09 06:12:47

如果您希望将来使用 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) ?>" />

我必须把...放在
每个 html/php 文件的开头 I
创建(使用 script/css/static
图像/等)?

是的。为将来可能需要引入的共享 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:

 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

  }

and the markup:

<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.)?

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 the cdn.php.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文