【问题标题】:Slim PHP framework 3.0: 404 error when accessing .phtml template routesSlim PHP 框架 3.0:访问 .phtml 模板路由时出现 404 错误
【发布时间】:2019-04-26 15:36:21
【问题描述】:

服务器配置:

  • Ubuntu 18.04
  • Apache 2.4.29
  • PostgreSQL 10.6
  • PHP 7.3(我使用的是 7.2 但系统已决定为我更新它,一旦我解决了当前的错误,我将回滚)
  • 作曲家
  • 超薄/超薄 3.0
  • Slim/php-view

我一直在使用 Slim PHP 框架构建一个 API,使用 gothinkster's repository 作为起点。尽管与渲染模板相关的所有代码都保持不变,但代码已经过大量修改。当我访问该网站的主页时,index.phtml 文件会正确呈现,但是从主页中的任何超链接都会导致404 错误,尽管.phtml 文件都位于同一目录中。

以下是我的代码节选,不包括不相关的配置。虽然我只显示了一个端点,但 API 定义了 10 个端点,所有这些都是不带参数的 get 请求,使用位于同一文件夹中的 .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();

设置文件返回一个包含对模板路径的引用的数组。

[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/ 内。

/ 已定义并成功返回位于模板目录中的index.phtml 文件,但是当我尝试从主页访问任何链接时,我收到404 错误。指向入门页面的锚点成功重定向到mywebsiteurl.com/getting-started,但模板文件未呈现,浏览器响应404 错误。

所有应用程序文件都私下存储在服务器上。我已经创建了从 [api_root]/public 文件夹的内容到我网站的 public_html 文件夹的符号链接。

我对@9​​87654343@目录路径的定义显然有问题,但我不知道如何纠正它。

编辑:

当我创建指向 public_html 文件夹的符号链接时,我忘记了包含隐藏文件(即.htaccess 文件)。我已经创建了这个符号链接,但现在当我尝试访问主页时,我看到了安装 apache 后显示的默认 apache2 页面。我会在这里指出,该网站位于付费服务器上。 Apache 配置为使用虚拟主机,并且我已正确编辑本地计算机上的主机文件。如前所述,如果没有.htaccess 文件,我可以查看主页,但找不到其他路线。当.htaccess 文件存在于public_html 文件夹中时,我无法查看主页或任何其他路径。

.htaccess:

<IfModule mod_rewrite.c>
  RewriteEngine On

  RewriteCond %{REQUEST_URI}::$1 ^(/.+)/(.*)::\2$
  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>

【问题讨论】:

  • 404 错误并不意味着您的应用找不到getting-started.phtml 文件。这可能与.htaccess 文件有关。您能否将该文件的内容添加到您的问题中?
  • @Nima 感谢您指出这一点。我已经更新了我的答案。正如我在编辑中解释的那样,我实际上忘记为 .htaccess 文件创建符号链接,但自从添加它后,我什至无法访问主页,我被重定向到默认的 apache 页面。

标签: php api routing http-status-code-404 slim


【解决方案1】:

这里的问题是我没有在服务器上启用mod_rewrite,所以我的.htaccess文件没有被读取。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-01-19
    • 2020-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-09-28
    • 1970-01-01
    相关资源
    最近更新 更多