首页 > 解决方案 > 尝试在 PHP 和 Apache 中使用 slim 框架时找不到 URL

问题描述

我在 linux 上使用 slim 框架和 php 和 apache2 有这段代码:

<?php
use \Psr\Http\Message\ServerRequestInterface as Request;
use \Psr\Http\Message\ResponseInterface as Response;

require '../../vendor/autoload.php';

$app = new \Slim\App;

#doesn't work results in URl not found in browser
$app->get('/hello','sayhi');

#works, when i just type in 'localhost/' into my webbrowser
$app->get('/',function()
{
return 'testing';
});





$app->run();

#functions
function sayhi()
{
  echo 'hello world again';
}

.htaccess(与我的 index.php 文件相同的目录)

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-d
RewriteCond %{REQUEST_FILENAME} !-f
RewriteRule . index.php [L]

000-default.conf 将 documentroot 设置为我的 php 文件和 .htaccess 文件所在的目录。

当我在我的网络浏览器中输入“localhost/”时,将执行匿名函数。因为如果服务器收到'/',它将执行此操作

但是,当我输入“localhost/hello”时,它会导致找不到 URL,当它应该在服务器接收到这种类型的 url 时执行“sayhi”函数时。

为什么要这样做?我的代码有问题吗,因为我不明白如何进行排序。

我还尝试将选项设置为我的 php 文件的目录

允许覆盖所有

但是,当我设置此选项时,这会导致 500 内部错误。我尝试遵循本指南:500 internal error guide无济于事。

我不知道如何排序,帮助?

标签: phpapacheslimubuntu-18.04

解决方案


您可以告诉 Apache 将所有路由重定向到索引。在您的虚拟主机或 .htaccess 文件中尝试类似的操作:

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA, L]

推荐阅读