php 精简\异常\HttpNotFound异常

6jygbczu  于 2023-02-07  发布在  PHP
关注(0)|答案(5)|浏览(167)

我正在创建一个新的Slim项目,并收到以下错误:Slim应用程序错误:由于以下错误,应用程序无法运行:
错误详细信息

Type: Slim\Exception\HttpNotFoundException
Code: 404
Message: Not found.
File: C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php
Line: 91

痕量

#0 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\RoutingMiddleware.php(57): Slim\Middleware\RoutingMiddleware->performRouting(Object(Slim\Psr7\Request))
#1 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(124): Slim\Middleware\RoutingMiddleware->process(Object(Slim\Psr7\Request), Object(Slim\Routing\RouteRunner))
#2 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\Middleware\ErrorMiddleware.php(89): class@anonymous->handle(Object(Slim\Psr7\Request))
#3 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(124): Slim\Middleware\ErrorMiddleware->process(Object(Slim\Psr7\Request), Object(class@anonymous))
#4 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\MiddlewareDispatcher.php(65): class@anonymous->handle(Object(Slim\Psr7\Request))
#5 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\App.php(174): Slim\MiddlewareDispatcher->handle(Object(Slim\Psr7\Request))
#6 C:\xampp\htdocs\MyApi\vendor\slim\slim\Slim\App.php(158): Slim\App->handle(Object(Slim\Psr7\Request))
#7 C:\xampp\htdocs\MyApi\public\index.php(18): Slim\App->run()
#8 {main}

下面是我的index.php

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
use Slim\Factory\AppFactory;

require '../vendor/autoload.php';

$app = AppFactory::create();
$app->addRoutingMiddleware();
$errorMiddleware = $app->addErrorMiddleware(true, true, true);

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

$app->run();

这可能是一个基本的问题。但我是新来的。需要帮助吗?

c0vxltue

c0vxltue1#

为了解决Slim 4的问题,我做了以下工作:
1/刚过

$app = AppFactory::create();

我补充道:

$app->setBasePath("/myapp/api"); // /myapp/api is the api folder (http://domain/myapp/api)

2/在我的. htaccess中:

RewriteEngine on  
RewriteCond %{ENV:REDIRECT_STATUS} !200 
RewriteRule ^api/(.*)$ api/index.php/$1

3/最后,为了处理'HttpNotFoundException' Slim异常,我添加了一个try/catch(我不知道为什么Slim不内部处理它=〉也许是为了给我们更多的可能性?)

try {
    $app->run();     
} catch (Exception $e) {    
  // We display a error message
  die( json_encode(array("status" => "failed", "message" => "This action is not allowed"))); 
}

希望能有所帮助

    • Edited =〉要强制Slim处理异常,请执行以下操作:**
$app = AppFactory::create();

增加:

$app->addErrorMiddleware(true, true, true);

参考:http://www.slimframework.com/docs/v4/middleware/body-parsing.html

wfypjpf4

wfypjpf42#

这对我很有效(基于Mandien的回答)。
我刚刚在setBasePath(...)中添加了index.php,不需要.htaccess。

$app->setBasePath("/myapp/public/index.php");

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

http://localhost/myapp/public/index.php/hello/world

  • 你好,世界 *

Linux、Apache、超薄4

qyzbxkaa

qyzbxkaa3#

我得到了同样的错误,让我描述一下我到底在做什么,然后它是如何工作的!
我在终端php -S localhost:8002上使用以下命令运行本地服务器
命令返回Document root is /Users/Codeus/Desktop/,当我导航到localhost:8002时发生错误
然后我再次从index.php或任何你想要运行的目录运行localhost,它对我来说工作得很好。例如,对我来说,它是两个级别,然后命令php -S localhost:8002返回Document root is /Users/Codeus/Desktop/myslim/src/public,这正是我的index.php文件所在的位置。希望这对你也有效。

ht4b089n

ht4b089n4#

如果你正在使用苗条和一个新手,然后尝试这个代码,它会工作
只需稍微修改一下代码。现在,您都有这样的代码:

$app->get('/hello/{name}', function (Request $request, Response $response, array $args) { ... }

将其更改为完整命名约定:

$app->get('/[my-appname]/public/hello/{name}', function (Request $request, Response $response, array $args) { .. }

完整代码

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

//here i used the url change to your project name on [my-appname]
 $app->get('/[my-appname]/public/hello/{name}', function (Request $request, 
 Response $response, array $args) {
    $name = $args['name'];
    $response->getBody()->write("Hello, $name");
    return $response;
 });

$app->run();
wgx48brx

wgx48brx5#

经过近3个小时的紧张浏览,我想出了一个解决方案,这将是从项目的根目录中降级的版本的Slim框架。我使用的是v4.3.0,由于一些奇怪的原因,不想表现的方式,它意味着和解决方案超出了我的理解。步骤如下:

  • 转到项目根目录,并在命令提示符窗口的文件路径中键入cmd。
  • 输入composer要求苗条/苗条:3.*
  • 完成后,您需要替换上预先存在的代码

public/index.php,并使用以下代码:

<?php
use Psr\Http\Message\ResponseInterface as Response;
use Psr\Http\Message\ServerRequestInterface as Request;
//use Slim\Factory\AppFactory;

require '../vendor/autoload.php';

$app = new \Slim\App;

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

$app->run();

上面的代码片段基本上是介绍页面的代码,但是来自Slim v3.12.3

相关问题