[试图在PHP和Apache中使用苗条框架时找不到URL

问题描述 投票:0回答:2

我在Linux上使用apache2使用苗条的框架和php编写此代码:

<?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';
}

当我在网络浏览器中输入'localhost /'时,将执行匿名功能。因为如果服务器接收到'/'

,它将执行该操作

但是,当我输入'localhost / hello'时,它导致找不到URL,当服务器接收到此类URL时应执行'sayhi'函数。

为什么这么做?我的代码有什么问题吗,因为我不知道如何对它进行排序。

php apache slim ubuntu-18.04
2个回答
0
投票

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


0
投票

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

RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA, L]
© www.soinside.com 2019 - 2024. All rights reserved.