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

问题描述 投票:0回答:3

我在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 [email protected]
  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>

有人可以给我一些指向我在这里做错什么的指示吗?谢谢!

php apache .htaccess mod-rewrite slim
3个回答
1
投票

要做的第一件事:

更改您的路线至此:

$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();

0
投票

尝试在API位置创建带有数据的index.php文件的地方创建.htaccess文件

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

0
投票

如Slim论坛上的this answer中所述:

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

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

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

  • 说明您所有路线的路径$app->get('/staging/', handler);等,或
  • 在创建应用后$app->SetBasePath('/staging');声明一次
© www.soinside.com 2019 - 2024. All rights reserved.