本地主机上的 Contao 4.9 - 没有加载资产
Contao 4.9 on localhost - no assets loaded
我刚刚尝试将本地 Contao CMS 4.4 升级到 4.9,之后所有资产都得到 404。
我是这样启动网站的:
cd web && php -S localhost:8190
例如:/assets/images/f/ReferenzenMaler-4cd0728f.jpg
- 我可以在 /assets/images/deferred/f
中看到一个 ReferenzenMaler-4cd0728f.jpg.json 并且该文件的内容指向 /files/cto_layout/...
[= 中的现有文件14=]
有人知道如何解决这个问题吗?
您需要确保任何对不存在的物理资源的请求都由 Contao 处理。例如如果你使用 nginx
并且你有一个以 jpg
、png
等结尾的任何 URL 的指令(这很常见),你需要确保这个指令还有一个 try_files
用于 index.php
.
因此,如果您想将 contao 与 build-in php webserver (php -S
) 一起使用,您需要像@fritzmg 提到的那样通过 index.php 路由资产。
事实证明,稍微调整过的 drupal .ht.router.php
也适用于此:
<?php
/**
* @file
* Router script for the built-in PHP web server.
*
* The built-in web server should only be used for development and testing as it
* has a number of limitations that makes running Drupal on it highly insecure
* and somewhat limited.
*
* Note that:
* - The server is single-threaded, any requests made during the execution of
* the main request will hang until the main request has been completed.
* - The web server does not enforce any of the settings in .htaccess in
* particular a remote user will be able to download files that normally would
* be protected from direct access such as .module files.
*
* The router script is needed to work around a bug in PHP, see
* https://bugs.php.net/bug.php?id=61286.
*
* Usage:
* php -S localhost:8888 .ht.router.php
*
* @see http://php.net/manual/en/features.commandline.webserver.php
*/
$url = parse_url($_SERVER['REQUEST_URI']);
if (strpos($url['path'], 'contao-manager.phar.php') > -1 || file_exists(__DIR__ . $url['path'])) {
// Serve the requested resource as-is, also if it calls for the contao manager.
return FALSE;
}
// Work around the PHP bug.
$path = $url['path'];
$script = 'index.php';
if (strpos($path, '.php') !== FALSE) {
// Work backwards through the path to check if a script exists. Otherwise
// fallback to index.php.
do {
$path = dirname($path);
if (preg_match('/\.php$/', $path) && is_file(__DIR__ . $path)) {
// Discovered that the path contains an existing PHP file. Use that as the
// script to include.
$script = ltrim($path, '/');
break;
}
} while ($path !== '/' && $path !== '.');
}
// Update $_SERVER variables to point to the correct index-file.
$index_file_absolute = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $script;
$index_file_relative = DIRECTORY_SEPARATOR . $script;
// SCRIPT_FILENAME will point to the router script itself, it should point to
// the full path of index.php.
$_SERVER['SCRIPT_FILENAME'] = $index_file_absolute;
// SCRIPT_NAME and PHP_SELF will either point to index.php or contain the full
// virtual path being requested depending on the URL being requested. They
// should always point to index.php relative to document root.
$_SERVER['SCRIPT_NAME'] = $index_file_relative;
$_SERVER['PHP_SELF'] = $index_file_relative;
// Require the script and let core take over.
require $_SERVER['SCRIPT_FILENAME'];
然后cd web && php -S localhost:8190 .ht.router.php
我刚刚尝试将本地 Contao CMS 4.4 升级到 4.9,之后所有资产都得到 404。
我是这样启动网站的:
cd web && php -S localhost:8190
例如:/assets/images/f/ReferenzenMaler-4cd0728f.jpg
- 我可以在 /assets/images/deferred/f
中看到一个 ReferenzenMaler-4cd0728f.jpg.json 并且该文件的内容指向 /files/cto_layout/...
[= 中的现有文件14=]
有人知道如何解决这个问题吗?
您需要确保任何对不存在的物理资源的请求都由 Contao 处理。例如如果你使用 nginx
并且你有一个以 jpg
、png
等结尾的任何 URL 的指令(这很常见),你需要确保这个指令还有一个 try_files
用于 index.php
.
因此,如果您想将 contao 与 build-in php webserver (php -S
) 一起使用,您需要像@fritzmg 提到的那样通过 index.php 路由资产。
事实证明,稍微调整过的 drupal .ht.router.php
也适用于此:
<?php
/**
* @file
* Router script for the built-in PHP web server.
*
* The built-in web server should only be used for development and testing as it
* has a number of limitations that makes running Drupal on it highly insecure
* and somewhat limited.
*
* Note that:
* - The server is single-threaded, any requests made during the execution of
* the main request will hang until the main request has been completed.
* - The web server does not enforce any of the settings in .htaccess in
* particular a remote user will be able to download files that normally would
* be protected from direct access such as .module files.
*
* The router script is needed to work around a bug in PHP, see
* https://bugs.php.net/bug.php?id=61286.
*
* Usage:
* php -S localhost:8888 .ht.router.php
*
* @see http://php.net/manual/en/features.commandline.webserver.php
*/
$url = parse_url($_SERVER['REQUEST_URI']);
if (strpos($url['path'], 'contao-manager.phar.php') > -1 || file_exists(__DIR__ . $url['path'])) {
// Serve the requested resource as-is, also if it calls for the contao manager.
return FALSE;
}
// Work around the PHP bug.
$path = $url['path'];
$script = 'index.php';
if (strpos($path, '.php') !== FALSE) {
// Work backwards through the path to check if a script exists. Otherwise
// fallback to index.php.
do {
$path = dirname($path);
if (preg_match('/\.php$/', $path) && is_file(__DIR__ . $path)) {
// Discovered that the path contains an existing PHP file. Use that as the
// script to include.
$script = ltrim($path, '/');
break;
}
} while ($path !== '/' && $path !== '.');
}
// Update $_SERVER variables to point to the correct index-file.
$index_file_absolute = $_SERVER['DOCUMENT_ROOT'] . DIRECTORY_SEPARATOR . $script;
$index_file_relative = DIRECTORY_SEPARATOR . $script;
// SCRIPT_FILENAME will point to the router script itself, it should point to
// the full path of index.php.
$_SERVER['SCRIPT_FILENAME'] = $index_file_absolute;
// SCRIPT_NAME and PHP_SELF will either point to index.php or contain the full
// virtual path being requested depending on the URL being requested. They
// should always point to index.php relative to document root.
$_SERVER['SCRIPT_NAME'] = $index_file_relative;
$_SERVER['PHP_SELF'] = $index_file_relative;
// Require the script and let core take over.
require $_SERVER['SCRIPT_FILENAME'];
然后cd web && php -S localhost:8190 .ht.router.php