【问题标题】:404 Error when using SLIM framework routing使用 SLIM 框架路由时出现 404 错误
【发布时间】:2015-07-27 17:15:17
【问题描述】:

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

我的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>

有人可以指点一下我在这里可能做错了什么吗?谢谢!

【问题讨论】:

标签: php apache .htaccess mod-rewrite slim


【解决方案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 -&gt; public -&gt; .htaccess, index.php

Project Folder -&gt; api -&gt; 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();

【讨论】:

    【解决方案2】:

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

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

    【讨论】:

    • 抱歉,现在是另一个错误:

      服务器遇到内部错误或配置错误,无法完成您的请求。

    • 由于某种原因,.htaccess 文件中的内容现在导致错误
    【解决方案3】:

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

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

    我们以这个网址为例:http://&lt;host&gt;/staging/

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

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

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2019-04-26
      • 1970-01-01
      • 2014-10-26
      • 1970-01-01
      • 1970-01-01
      • 2014-08-27
      • 1970-01-01
      • 2012-09-02
      相关资源
      最近更新 更多