【问题标题】:URL not found when trying to use slim framework with PHP and Apache尝试在 PHP 和 Apache 中使用 slim 框架时找不到 URL
【发布时间】:2020-02-09 02:02:44
【问题描述】:

我有这个代码在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 无济于事。

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

【问题讨论】:

  • 您使用的是哪个版本的 os Slim?它看起来像 slim 3,但你的应用程序实例很奇怪(slim 3 需要提供配置)
  • 我使用的是 Slim 3.12 版本
  • .htaccess文件的内容是什么?这个文件存在吗?
  • 是的,我已经更新了我的问题并将其包含在问题描述中。
  • @Entilore 我有 3.12 并按照 slim 版本 3 的代码示例,与网站的实现风格完全相同:slimframework.com/docs/v3/tutorial/first-app.html

标签: php apache slim ubuntu-18.04


【解决方案1】:

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

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

【讨论】:

  • 在您的虚拟主机中您需要添加:AllowOverride All 它会说 Apache 接受 .htaccess
  • 试过了,结果是500内部错误。知道为什么吗?
  • 你能发布你的虚拟主机吗?你有任何 apache 错误日志吗?
【解决方案2】:

我认为您需要配置 apache,以便所有请求都进入您的代码所在的 index.php。似乎当您请求 localhost/hello 时,您的网络服务器会尝试查找文件 hello。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 2018-11-22
  • 2013-12-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2016-04-04
  • 2012-03-15
相关资源
最近更新 更多