沃梦达 / 编程问答 / php问题 / 正文

SlimExceptionHttpNotFoundException异常

SlimExceptionHttpNotFoundException(SlimExceptionHttpNotFoundException异常)

本文介绍了SlimExceptionHttpNotFoundException异常的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

错误详细信息

Type: SlimExceptionHttpNotFoundException
Code: 404
Message: Not found.
File: C:xampphtdocsMyApivendorslimslimSlimMiddlewareRoutingMiddleware.php
Line: 91

跟踪

#0 C:xampphtdocsMyApivendorslimslimSlimMiddlewareRoutingMiddleware.php(57): SlimMiddlewareRoutingMiddleware->performRouting(Object(SlimPsr7Request))
#1 C:xampphtdocsMyApivendorslimslimSlimMiddlewareDispatcher.php(124): SlimMiddlewareRoutingMiddleware->process(Object(SlimPsr7Request), Object(SlimRoutingRouteRunner))
#2 C:xampphtdocsMyApivendorslimslimSlimMiddlewareErrorMiddleware.php(89): class@anonymous->handle(Object(SlimPsr7Request))
#3 C:xampphtdocsMyApivendorslimslimSlimMiddlewareDispatcher.php(124): SlimMiddlewareErrorMiddleware->process(Object(SlimPsr7Request), Object(class@anonymous))
#4 C:xampphtdocsMyApivendorslimslimSlimMiddlewareDispatcher.php(65): class@anonymous->handle(Object(SlimPsr7Request))
#5 C:xampphtdocsMyApivendorslimslimSlimApp.php(174): SlimMiddlewareDispatcher->handle(Object(SlimPsr7Request))
#6 C:xampphtdocsMyApivendorslimslimSlimApp.php(158): SlimApp->handle(Object(SlimPsr7Request))
#7 C:xampphtdocsMyApipublicindex.php(18): SlimApp->run()
#8 {main}

这是我的index.php

<?php
use PsrHttpMessageResponseInterface as Response;
use PsrHttpMessageServerRequestInterface as Request;
use SlimFactoryAppFactory;

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

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

推荐答案

要解决超薄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/Finaly为了处理‘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"))); 
}

希望能有所帮助

**Edted=>;强制Slim处理异常,在:**

之后
$app = AppFactory::create();

添加:

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

引用:http://www.slimframework.com/docs/v4/middleware/body-parsing.html

这篇关于SlimExceptionHttpNotFoundException异常的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持编程学习网!

本文标题为:SlimExceptionHttpNotFoundException异常

基础教程推荐