【问题标题】:can't add route in Slim无法在 Slim 中添加路线
【发布时间】:2015-06-30 10:49:55
【问题描述】:

我的问题是我无法添加'/' 以外的路线。 如果我将 / 更改为 /hello 我会收到 404 错误。我认为我的路径有误或.htaccess

我的.htaccess:

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

这是我的代码和我的项目结构

require '../../vendor/slim/slim/Slim/Slim.php';
\Slim\Slim::registerAutoloader();
require_once '../../vendor/autoload.php';
$app = new \Slim\Slim();
$app->get('/hello', function () {   //'/' works fine
    echo "Hello";
});
$app->run();

【问题讨论】:

  • 您访问的网址是否正确?所以localhost/hello?你的代码很好。当然/ 会回复404,因为您已将其更改为/hello
  • 您的文档根目录指向哪个文件夹?我猜你需要使用localhost/DEVOLO_UI/hello 以及显示你的开发者工具(网络选项卡)的内容?
  • 供应商文件夹位于 localhost/API/v1 但我的 index.php 位于 localhost/API/v1/DEVOLO_UI/form
  • yout htaccess 说什么?看起来有问题。您的 htacces 必须将所有请求转发到该 index.php。

标签: php .htaccess url-routing slim front-controller


【解决方案1】:

在您的.htaccess 文件中,您有以下规则:

RewriteRule ^ index.php [QSA,L] –

由于您没有为index.php 指定路径,Apache 将尝试在当前目录中加载index.php 文件。但由于该文件不存在,您将收到错误 404 的响应。

但由于.htaccess 文件不在您正在访问的目录下,因此它甚至没有被服务器加载。您需要执行以下操作之一:

  1. index.php 文件移动到项目根目录,正如人们在 cmets 中针对您的问题所建议的那样(这是最好的解决方案)。
  2. .htaccess移动到与index.php相同的目录下(好像是DEVOLO_UI/form)。

顺便问一下,你考虑过只使用 Composer 的自动加载吗?你不需要调用两个自动加载:Slim 和 Composer。在您的index.php 中,您可能会这样写:

// Set the current dir to application's root.
// You may have to change the path depending on 
// where you'll keep your index.php.
$path = realpath('../../');
chdir($path);

require 'vendor/autoload.php';

$app = new \Slim\Slim();

// Your routes followed by $app->run();
// ...

【讨论】:

    猜你喜欢
    • 2013-04-06
    • 1970-01-01
    • 2023-03-28
    • 1970-01-01
    • 2023-03-17
    • 1970-01-01
    • 2019-07-22
    • 2018-03-08
    • 1970-01-01
    相关资源
    最近更新 更多