我在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'函数。
为什么这么做?我的代码有什么问题吗,因为我不知道如何对它进行排序。
我认为您需要配置apache,以便所有请求都可以进入代码所在的index.php。似乎当您请求localhost/hello
时,您的Web服务器会尝试查找文件hello。
您可以告诉Apache将所有路由重定向到索引。尝试在您的虚拟主机或.htaccess文件中进行类似操作:
RewriteEngine on
RewriteCond %{REQUEST_FILENAME} !-f
RewriteCond %{REQUEST_FILENAME} !-d
RewriteRule ^(.*)$ /index.php [QSA, L]