使用 Slim Framework 创建 Rest Api 的 index.php 文件出错
Error in index.php file to create Rest Api using Slim Framework
我复制了 slimeframework qiuck 代码并粘贴到 index.php 但是当我 运行 在 localhost(http://localhost/MyApiFirstTry/public/hello/testMessage),
它显示了一些错误..
像这样
Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:93 Stack trace: #0
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\Routing\RouteRunner.php(72):
Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81):
Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\App.php(211):
Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\App.php(195):
Slim\App->handle(Object(Slim\Psr7\Request)) #4 C:\xampp\htdocs\MyApiFirstTry\public\index.php(17): Slim\App->run() #5 {main} thrown in
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 93
我该如何解决错误..
问题
您当前在 XAMPP 中的文档根目录是 http://localhost/
。但是在你的应用中,它应该是http://localhost/MyApiFirstTry/public/
。这就是为什么您会收到 HttpNotFoundException 异常。
当你在你的路线前面加上 /MyApiFirstTry/public/
:
时它会起作用
app->get('/MyApiFirstTry/public/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
但事实并非如此。你不应该这样做。在 Slim documentation:
It is typical to use the front-controller pattern to funnel
appropriate HTTP requests received by your web server to a single PHP
file.
您应该配置您的 Web 服务器以将 HTTP 请求发送到您的 PHP 前端控制器文件 (/index.php
)。
解决方案
转到 C:\xampp\htdocs\MyApiFirstTry\
目录并 运行 在终端中输入以下命令以启动本地主机 Web 服务器:
php -S localhost:8888 -t public
然后在浏览器中导航至 http://localhost:8888
。
详细了解 PHP built-in server。
在我的例子中,我遇到了类似的问题,所以我在示例 Web 服务中添加了以下代码
$middleware = $app->addErrorMiddleware(true,true,true);
所以我最终的示例代码输出是
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
//print_r($app);
$middleware = $app->addErrorMiddleware(true,true,true);
$app->get('/retrofit2/public/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello how are you $name");
return $response;
});
$app->run();
我复制了 slimeframework qiuck 代码并粘贴到 index.php 但是当我 运行 在 localhost(http://localhost/MyApiFirstTry/public/hello/testMessage),
它显示了一些错误..
像这样
Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:93 Stack trace: #0
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\Routing\RouteRunner.php(72):
Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request)) #1
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81):
Slim\Routing\RouteRunner->handle(Object(Slim\Psr7\Request)) #2
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\App.php(211):
Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request)) #3 C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\App.php(195):
Slim\App->handle(Object(Slim\Psr7\Request)) #4 C:\xampp\htdocs\MyApiFirstTry\public\index.php(17): Slim\App->run() #5 {main} thrown in
C:\xampp\htdocs\MyApiFirstTry\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 93
我该如何解决错误..
问题
您当前在 XAMPP 中的文档根目录是 http://localhost/
。但是在你的应用中,它应该是http://localhost/MyApiFirstTry/public/
。这就是为什么您会收到 HttpNotFoundException 异常。
当你在你的路线前面加上 /MyApiFirstTry/public/
:
app->get('/MyApiFirstTry/public/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello, $name");
return $response;
});
但事实并非如此。你不应该这样做。在 Slim documentation:
It is typical to use the front-controller pattern to funnel appropriate HTTP requests received by your web server to a single PHP file.
您应该配置您的 Web 服务器以将 HTTP 请求发送到您的 PHP 前端控制器文件 (/index.php
)。
解决方案
转到 C:\xampp\htdocs\MyApiFirstTry\
目录并 运行 在终端中输入以下命令以启动本地主机 Web 服务器:
php -S localhost:8888 -t public
然后在浏览器中导航至 http://localhost:8888
。
详细了解 PHP built-in server。
在我的例子中,我遇到了类似的问题,所以我在示例 Web 服务中添加了以下代码
$middleware = $app->addErrorMiddleware(true,true,true);
所以我最终的示例代码输出是
<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;
require __DIR__ . '/../vendor/autoload.php';
$app = AppFactory::create();
//print_r($app);
$middleware = $app->addErrorMiddleware(true,true,true);
$app->get('/retrofit2/public/hello/{name}', function (Request $request, Response $response, array $args) {
$name = $args['name'];
$response->getBody()->write("Hello how are you $name");
return $response;
});
$app->run();