繁体   English   中英

使用 SLIM 框架路由时出现 404 错误

[英]404 Error when using SLIM framework routing

我在http://www.example.com/API/有我的 SLIM PHP 应用程序

我的index.php有路由,如下所示:

$app->get('/getstudents', function() use($app) {
    $db = new DbHandler();
    $result = $db->getAllStudents();

    if ($result === false) {
        echo "Failed to fetch";
    } else {
        echo json_encode($result);
    }
});

$app->run();

当我使用curl http://example.com/API/getstudents尝试 url 时,我收到错误在此服务器上找不到请求的 URL /API/getstudents。

如果我使用curl http://example.com/API/index.php/getstudents ,则会按预期返回学生列表。 我在谷歌上搜索并了解到这可能是因为.htaccess文件和/或虚拟主机设置。

所以我将.htaccessAPI/vendor/slim/slim/.htaccess复制到 API 文件夹,因此它与index.php位于同一文件夹中。 在这个文件中,我有:

# Some hosts may require you to use the `RewriteBase` directive.
# If you need to use the `RewriteBase` directive, it should be the
# absolute physical path to the directory that contains this htaccess file.
#
# RewriteBase /

RewriteEngine On

RewriteBase /API/

RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule ^ index.php [QSA,L]

没有运气。 我是否需要同时更改 API 目录和 API/vendor/slim/slim/ 中.htaccess文件的内容?

我也不完全确定我需要对我的虚拟主机文件做什么。 在 /etc/apache2/sites-available 我正在编辑example.com.conf ,内容是:

<VirtualHost *:80>
  # Admin email, Server Name (domain name), and any aliases
  ServerAdmin webmaster@example.co
  ServerName  www.example.co
  ServerAlias example.co
  # Index file and Document Root (where the public files are located)
  DirectoryIndex index.html index.php
  DocumentRoot /var/www/html/example.co/public_html
  # Log file locations
  LogLevel warn
  ErrorLog  /var/www/html/example.co/logs/error.log
  CustomLog /var/www/html/example.co/logs/access.log combined
  <Directory "/var/www/html/example.co/public_html">
        AllowOverride All
        Order allow,deny
        Allow from all
  </Directory>
</VirtualHost>

有人可以给我一些关于我在这里可能做错的指示吗? 谢谢!

要做的第一件事:

将您的路线更改为:

$app->get('/API/getstudents', function() use($app) {

如果这不起作用,请执行以下操作:

解决这个问题,让你的路线是这样的:

$app->get('/api/book', function () {
    header('Content-Type: application/json; charset=utf-8');
    $data = Book::getBookObjects();
    echo $data;
});

这是我的一个项目的示例,您可以使用 API 而不是 api。

.htaccess 文件:

RewriteEngine On
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^ index.php [QSA,L]

我的项目的工作树是这样的:

Project Folder -> public -> .htaccess, index.php

Project Folder -> api -> book

调整您的代码:

$app->get('/api/getstudents', function() use($app) {
    $db = new DbHandler();
    $result = $db->getAllStudents();
    if ($result === false) {
        echo "Failed to fetch";
    } else {
        echo json_encode($result);
    }
});

$app->run();

尝试在 API 位置创建.htaccess文件,其中包含数据的 index.php 文件

RewriteEngine On 
RewriteCond %{REQUEST_FILENAME} !-f 
RewriteRule ^(.*)$ %{ENV:BASE}index.php [QSA,L]

正如 Slim 论坛上的这个答案所述:

如果您的 Slim 应用程序不在服务器根路径 ( http://<host>/[index.php] ) 上,则必须说明您的路由的完整路径。

让我们以这个 url 为例: http://<host>/staging/

您有两种选择来实现我们的目标:

  • 说明所有路由中的路径$app->get('/staging/', handler); 等等,或
  • 在应用程序创建后声明一次$app->SetBasePath('/staging');

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM