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异常
基础教程推荐
猜你喜欢
- XAMPP 服务器不加载 CSS 文件 2022-01-01
- 在 PHP 中强制下载文件 - 在 Joomla 框架内 2022-01-01
- 通过 PHP SoapClient 请求发送原始 XML 2021-01-01
- 在多维数组中查找最大值 2021-01-01
- WooCommerce 中选定产品类别的自定义产品价格后缀 2021-01-01
- mysqli_insert_id 是否有可能在高流量应用程序中返回 2021-01-01
- Libpuzzle 索引数百万张图片? 2022-01-01
- 超薄框架REST服务两次获得输出 2022-01-01
- 如何在 PHP 中的请求之间持久化对象 2022-01-01
- 在 Woocommerce 中根据运输方式和付款方式添加费用 2021-01-01