Slim PHP 框架 3.0: 访问 .phtml 模板路由时出现 404 错误
Slim PHP framework 3.0: 404 error when accessing .phtml template routes
服务器配置:
- Ubuntu 18.04
- 阿帕奇 2.4.29
- PostgreSQL 10.6
- PHP 7.3(我用的是7.2,但是系统决定帮我更新了,等我解决了当前的bug我再回滚)
- 作曲家
- Slim/Slim 3.0
- Slim/php-view
我一直在使用 Slim PHP 框架构建一个 API,使用 gothinkster's repository 作为起点。代码已经过大量修改,但与渲染模板相关的所有代码都保持不变。当我访问该网站的主页时,index.phtml
文件被正确呈现,但是从主页上的任何超链接都会导致 404
错误,尽管 .phtml
文件都位于同一目录中。
以下是我的代码摘录,不包括不相关的配置。虽然我只显示了一个端点,但 API 定义了 10 个端点,所有端点都是不带参数的获取请求,使用位于同一文件夹中的 .phtml
个文件。
使用 index.php
文件中的 require 语句调用设置、路由和依赖项。
/[api_root]/public/index.php:
if (PHP_SAPI == 'cli-server') {
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
设置文件returns一个包含对模板路径的引用的数组。
[api_root]/src/settings.php:
'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]
模板已分配给 dependencies.php
文件中的渲染器。
[api_root]/src/dependencies.php:
$container = $app->getContainer();
// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new Slim\Views\PhpRenderer($settings['template_path']);
};
.phtml
个模板在 get
个请求中呈现。
[api_root]/src/routes.php
$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});
getting-started.phtml
文件位于 [api_root]/templates/
。
根 /
已定义并成功 returns 位于模板目录中的 index.phtml
文件,但是当我尝试从主页访问任何链接时,我收到一个 404
错误。指向入门页面的锚点成功重定向到mywebsiteurl.com/getting-started
,但未呈现模板文件,浏览器响应404
错误。
所有应用程序文件都私密地存储在服务器上。我已经创建了从 [api_root]/public
文件夹的内容到我网站的 public_html
文件夹的符号链接。
我对 /templates
目录路径的定义显然有问题,但我不知道如何纠正它。
编辑:
当我创建指向 public_html 文件夹的符号链接时,我忘记包含隐藏文件(即 .htaccess
文件)。我已经创建了这个符号链接,尽管现在当我尝试访问主页时,我看到了默认的 apache2 页面,该页面在安装 apache 后显示。我会在这里指出,该网站驻留在付费服务器上。 Apache 配置为使用虚拟主机,并且我已正确编辑本地计算机上的主机文件。如前所述,没有 .htaccess
文件我可以查看主页,但找不到其他路由。当.htaccess
文件存在于public_html文件夹中时,我无法查看主页或任何其他路径。
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
这里的问题是我没有在服务器上启用 mod_rewrite
,所以我的 .htaccess
文件没有被读取。
服务器配置:
- Ubuntu 18.04
- 阿帕奇 2.4.29
- PostgreSQL 10.6
- PHP 7.3(我用的是7.2,但是系统决定帮我更新了,等我解决了当前的bug我再回滚)
- 作曲家
- Slim/Slim 3.0
- Slim/php-view
我一直在使用 Slim PHP 框架构建一个 API,使用 gothinkster's repository 作为起点。代码已经过大量修改,但与渲染模板相关的所有代码都保持不变。当我访问该网站的主页时,index.phtml
文件被正确呈现,但是从主页上的任何超链接都会导致 404
错误,尽管 .phtml
文件都位于同一目录中。
以下是我的代码摘录,不包括不相关的配置。虽然我只显示了一个端点,但 API 定义了 10 个端点,所有端点都是不带参数的获取请求,使用位于同一文件夹中的 .phtml
个文件。
使用 index.php
文件中的 require 语句调用设置、路由和依赖项。
/[api_root]/public/index.php:
if (PHP_SAPI == 'cli-server') {
$url = parse_url($_SERVER['REQUEST_URI']);
$file = __DIR__ . $url['path'];
if (is_file($file)) {
return false;
}
}
require __DIR__ . '/../vendor/autoload.php';
session_start();
// Instantiate the app
$settings = require __DIR__ . '/../src/settings.php';
$app = new \Slim\App($settings);
// Set up dependencies
require __DIR__ . '/../src/dependencies.php';
// Register middleware
require __DIR__ . '/../src/middleware.php';
// Register routes
require __DIR__ . '/../src/routes.php';
// Run app
$app->run();
设置文件returns一个包含对模板路径的引用的数组。
[api_root]/src/settings.php:
'settings' => [
'renderer' => [
'template_path' => __DIR__ . '/../templates/',
]
]
模板已分配给 dependencies.php
文件中的渲染器。
[api_root]/src/dependencies.php:
$container = $app->getContainer();
// view renderer
$container['renderer'] = function ($c) {
$settings = $c->get('settings')['renderer'];
return new Slim\Views\PhpRenderer($settings['template_path']);
};
.phtml
个模板在 get
个请求中呈现。
[api_root]/src/routes.php
$app->get('/getting-started',
function (Request $request, Response $response) {
return $this->renderer->render($response, 'getting-started.phtml');
});
getting-started.phtml
文件位于 [api_root]/templates/
。
根 /
已定义并成功 returns 位于模板目录中的 index.phtml
文件,但是当我尝试从主页访问任何链接时,我收到一个 404
错误。指向入门页面的锚点成功重定向到mywebsiteurl.com/getting-started
,但未呈现模板文件,浏览器响应404
错误。
所有应用程序文件都私密地存储在服务器上。我已经创建了从 [api_root]/public
文件夹的内容到我网站的 public_html
文件夹的符号链接。
我对 /templates
目录路径的定义显然有问题,但我不知道如何纠正它。
编辑:
当我创建指向 public_html 文件夹的符号链接时,我忘记包含隐藏文件(即 .htaccess
文件)。我已经创建了这个符号链接,尽管现在当我尝试访问主页时,我看到了默认的 apache2 页面,该页面在安装 apache 后显示。我会在这里指出,该网站驻留在付费服务器上。 Apache 配置为使用虚拟主机,并且我已正确编辑本地计算机上的主机文件。如前所述,没有 .htaccess
文件我可以查看主页,但找不到其他路由。当.htaccess
文件存在于public_html文件夹中时,我无法查看主页或任何其他路径。
.htaccess:
<IfModule mod_rewrite.c>
RewriteEngine On
RewriteCond %{REQUEST_URI}:: ^(/.+)/(.*)::$
RewriteCond %{HTTP_HOST} ^(www\.)?mywebsiteurl.com
RewriteRule ^(.*) - [E=BASE:%1]
# If the above doesn't work you might need to set the `RewriteBase` directive manually, it should be the
# absolute physical path to the directory that contains this htaccess file.
# RewriteBase /
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]
</IfModule>
这里的问题是我没有在服务器上启用 mod_rewrite
,所以我的 .htaccess
文件没有被读取。