有很多方法可以做到这一点。
在我看来,Assetic 浪费了计算性能,非常不适合这个简单的问题。
如前所述,问题是从模块访问 /public 。
我的解决方案如下:
编辑 htdocs/yoursite/public/.htaccess 以在 RewriteEngine On 之后添加此行:
RewriteRule ^resource/([a-zA-Z0-9\.\-]+)/([a-zA-Z0-9\.\-_\/]+)$ index.php?action=resource&module=$1&path=$2 [QSA,L]
编辑 htdocs/yoursite/public/index.php 并在 chdir(dirname( DIR ));之后添加此代码:
if (isset($_GET['action']) && $_GET['action'] == "resource") {
$module = $_GET['module'];
$path = $_GET['path'];
if (!ctype_alnum($module))
die("Module name must consist of only alphanumeric characters");
$filetype = pathinfo($path, PATHINFO_EXTENSION);
$mimetypes = array(
'js' => "text/javascript",
'css' => "text/css",
'jpg' => "image/jpeg",
'jpeg' => "image/jpeg",
'png' => "image/png"
);
if (!isset($mimetypes[$filetype]))
die(sprintf("Unrecognized file extension '%s'. Supported extensions: %s.", htmlspecialchars($filetype, ENT_QUOTES), implode(", ", array_keys($mimetypes))));
$currentDir = realpath(".");
$destination = realpath("module/$module/public/$path");
if (!$destination)
die(sprintf("File not found: '%s'!", htmlspecialchars("module/$module/public/$path", ENT_QUOTES)));
if (substr($destination, 0, strlen($currentDir)) != $currentDir)
die(sprintf("Access to '%s' is not allowed!", htmlspecialchars($destination, ENT_QUOTES)));
header(sprintf("Content-type: %s", $mimetypes[$filetype]));
readfile("module/$module/public/$path", FALSE);
die();
}
用法:/resource/moduleName/path
示例:http:
//yoursite.com/resource/Statistics/css/style.css将从 yoursite/module/Statistics/public/css/style.css 读取实际的 css。
它快速、安全、不需要您在配置中指定任何路径、不需要安装、不依赖 3rd-party 维护,并且不需要帮助。只需从任何地方访问 /resource !享受 :)