XAMPP上的Slim应用程序抛出“未找到”异常

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

我尝试在XAMPP上运行我的Slim框架项目,并使用Apache configuration from Slim framework website

当我打开此URLhttp://localhost:8081/SlimAPIProject/public/hello/ayad我收到此错误:

Fatal error: Uncaught Slim\Exception\HttpNotFoundException: Not found. in C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php:93 Stack trace:
#0 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Routing\RouteRunner.php(72): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Http\ServerRequest))
#1 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\MiddlewareDispatcher.php(81): Slim\Routing\RouteRunner->handle(Object(Slim\Http\ServerRequest))
#2 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\App.php(211): Slim\MiddlewareDispatcher->handle(Object(Slim\Http\ServerRequest))
#3 C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\App.php(195): Slim\App->handle(Object(Slim\Http\ServerRequest))
#4 C:\xampp\htdocs\SlimAPIProject\public\index.php(16): Slim\App->run()
#5 {main} thrown in C:\xampp\htdocs\SlimAPIProject\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php on line 93

这是我的index.php

<?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();

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
});

$app->run();
.htaccess xampp slim
1个回答
0
投票

如果在子目录中运行Slim,则可能会发生这种情况。 Slim需要知道应将URL的哪一部分视为您应用的基本URL,以便将其后的所有内容都视为一条路线。

我看到了很多解决方案,但没有一个对我有用。

另外,您希望您的应用程序具有便携性。这样,当您部署它时,您就不需要摆弄生产服务器上一些不同的设置。因此,我提出了以下解决方案,该解决方案不需要在Slim主应用程序的入口点之外进行任何更改,无需htaccess

<?php

...

// Instantiate App
$app = AppFactory::create();

// Set the base path of the Slim App
$basePath = str_replace('/' . basename(__FILE__), '', $_SERVER['SCRIPT_NAME']);
$app = $app->setBasePath($basePath);


这将动态计算应该用作基本浴的值,并且如果您将脚本移到其他位置(如果您对其进行重命名)将可以使用。

© www.soinside.com 2019 - 2024. All rights reserved.