0

服务器配置:

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

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

以下是我的代码节选,不包括不相关的配置。虽然我只显示了一个端点,但 API 定义了 10 个端点,所有这些都是不带参数的获取请求,使用.phtml位于同一文件夹中的文件。

使用文件中的 require 语句调用设置、路由和依赖项index.php

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

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

编辑:

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

.ht 访问:

<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>
4

1 回答 1

0

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

于 2018-11-29T07:09:25.510 回答