简体   繁体   English

尝试将超薄框架与 PHP 和 Apache 一起使用时未找到 URL

[英]URL not found when trying to use slim framework with PHP and Apache

I have this code using the slim framework and php with apache2 on linux:我在 linux 上使用超薄框架和 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 (same directory as my index.php file) .htaccess(与我的索引相同的目录。php 文件)

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

000-default.conf has the documentroot set to the directory of where my php file and .htaccess file. 000-default.conf 将 documentroot 设置为我的 php 文件和 .htaccess 文件所在的目录。

When i type in 'localhost/' into my web browser the anonymous function is executed.当我在我的 web 浏览器中输入“localhost/”时,匿名 function 被执行。 As that's what it will do if the server recieves '/'因为如果服务器收到'/',它将执行此操作

However when i type in 'localhost/hello' it results in URL not found, when it should execute the 'sayhi' function when the server receives this type of url.但是,当我输入“localhost/hello”时,它会导致 URL 找不到,当它应该执行“sayhi”时 function 当服务器收到这种类型的 Z572D4E421E5E6B29BC11D815E.

Why is it doing this?为什么要这样做? is there something wrong in my code, as i don't understand how to go about sorting this.我的代码有什么问题吗,因为我不明白如何对 go 进行排序。

I also tried setting the option to the directory of my php file我还尝试将选项设置为我的 php 文件的目录

AllowOverride All允许覆盖所有

However this results in a 500 Internal error, when i have this option set.但是,当我设置此选项时,这会导致 500 内部错误。 I tried following this guide: 500 internal error guide to no avail.我尝试遵循本指南: 500 internal error guide无济于事。

I have no idea how to sort this, help?我不知道如何排序,帮助?

You can tell Apache to redirect all routes to index.您可以告诉 Apache 将所有路由重定向到索引。 Try something like in your vhost or in a .htaccess file:在您的虚拟主机或 .htaccess 文件中尝试类似的操作:

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

I think you need to configure apache so that all requests would go into index.php in which your code are located.我认为您需要配置 apache 以便所有请求都将 go 放入您的代码所在的 index.php 中。 It seems that when you request localhost/hello your web server tries to find file hello.似乎当您请求localhost/hello时,您的 web 服务器会尝试查找文件 hello。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM