Slim仍处理错误,即使设置为不

时间:2017-02-23 13:04:48

标签: php slim

这是我的主要Slim app实例化代码:

$slimSettings = [
    'settings' => [
        'displayErrorDetails' => false,
        'routerCacheFile'     => false  # Settings::ABSPATH . '/tmp/route.cache'
    ],
];

# Initialize
$app = new \Slim\App( $slimSettings );

# Create container
$container = $app->getContainer();

# Register component on container
$container[ 'view' ] = function( $container ) {
    $renderer = new \Slim\Views\PhpRenderer();
    $renderer->setAttributes( [ 'absPath' => Settings::ABSPATH ] ); # Variable accessible from all templates

    return $renderer;
};

# Remove error handling from slim, so custom error handling is used.
unset( $app->getContainer()[ 'errorHandler' ] );

您可以看到最后一行应该取消设置errorHandler。 然而,在控制器中,我举例说明了一个函数:

public function index( $request, Response $response )

我忘了设置回应'使用语句,因此它创建了一个错误(很容易修复,但不是重点)。但这个错误是由苗条处理的。这是为什么 ?它不应该处理未设置语句中的错误吗?

更新:错误:

详细

Slim Application Error
The application could not run because of the following error:

Type: TypeError
Message: Argument 2 passed to Rib\Src\Controllers\HomeController::index() must be an instance of Rib\Src\Controllers\Response, instance of Slim\Http\Response given
File: /Users/ME/Documents/Apps_And_Sites/PHP_Apps/ribklein/src/Controllers/HomeController.php
Line: 19

2 个答案:

答案 0 :(得分:0)

从您发布的错误输出看起来,Slim没有处理错误,而是使用了默认的php错误处理程序。

Slim错误输出看起来像这样:

Slim Application Error

The application could not run because of the following error:
Details
Type: TypeError
Message: Argument 1 passed to Sleeti\Controllers\Authentication\a() must implement interface Psr\Http\Message\ServerRequestInterface, array given, called in C:\Source\Web\PHP\Apps\SlimPomf\src\Controllers\Authentication\AuthenticationController.php on line 62
File: C:\Source\Web\PHP\Apps\SlimPomf\src\Controllers\Authentication\AuthenticationController.php
Line: 58
Trace

#0 C:\Source\Web\PHP\Apps\SlimPomf\src\Controllers\Authentication\AuthenticationController.php(62): Sleeti\Controllers\Authentication\a(Array)
#1 [internal function]: Sleeti\Controllers\Authentication\AuthenticationController->postLogin(Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#2 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\Handlers\Strategies\RequestResponse.php(41): call_user_func(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#3 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\Route.php(340): Slim\Handlers\Strategies\RequestResponse->__invoke(Array, Object(Slim\Http\Request), Object(Slim\Http\Response), Array)
#4 C:\Source\Web\PHP\Apps\SlimPomf\src\Middleware\GuestMiddleware.php(27): Slim\Route->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#5 [internal function]: Sleeti\Middleware\GuestMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#6 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Sleeti\Middleware\GuestMiddleware), Array)
#7 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#8 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\Route))
#9 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116): Slim\Route->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#10 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\Route.php(316): Slim\Route->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#11 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\App.php(438): Slim\Route->run(Object(Slim\Http\Request), Object(Slim\Http\Response))
#12 C:\Source\Web\PHP\Apps\SlimPomf\src\Middleware\LocalizationMiddleware.php(29): Slim\App->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response))
#13 [internal function]: Sleeti\Middleware\LocalizationMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#14 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Sleeti\Middleware\LocalizationMiddleware), Array)
#15 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#16 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Slim\App))
#17 C:\Source\Web\PHP\Apps\SlimPomf\src\Middleware\LoggerMiddleware.php(36): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#18 [internal function]: Sleeti\Middleware\LoggerMiddleware->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#19 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\DeferredCallable.php(43): call_user_func_array(Object(Sleeti\Middleware\LoggerMiddleware), Array)
#20 [internal function]: Slim\DeferredCallable->__invoke(Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#21 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(67): call_user_func(Object(Slim\DeferredCallable), Object(Slim\Http\Request), Object(Slim\Http\Response), Object(Closure))
#22 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\MiddlewareAwareTrait.php(116): Slim\App->Slim\{closure}(Object(Slim\Http\Request), Object(Slim\Http\Response))
#23 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\App.php(332): Slim\App->callMiddlewareStack(Object(Slim\Http\Request), Object(Slim\Http\Response))
#24 C:\Source\Web\PHP\Apps\SlimPomf\vendor\slim\slim\Slim\App.php(293): Slim\App->process(Object(Slim\Http\Request), Object(Slim\Http\Response))
#25 C:\Source\Web\PHP\Apps\SlimPomf\public\index.php(5): Slim\App->run()
#26 {main}

答案 1 :(得分:0)

  

传递给Rib \ Src \ Controllers \ HomeController :: index()的参数2必须是Rib \ Src \ Controllers \ Response的实例,Slim \ Http \ Response的实例

这是名称空间声明错误。答案

在顶部的控制器文件(HomeController)中添加以下内容:

use \Psr\Http\Message\ResponseInterface as Response;

<强>更新

抱歉,我误解了这个问题。所以,你想知道为什么错误是由Slim处理的。

这是因为错误是PHP运行时错误。而Slim有特殊的处理程序。在容器中,它存储在phpErrorHandler键下。默认情况下,它会返回500响应代码,当然,您可以定义自己的错误处理程序。

因此,要禁用Slim处理PHP错误,请取消设置$container['phpErrorHandler']

重要提示:此处理程序仅限PHP7 +,因为PHP Error class was introduced in PHP7.0

您可以阅读有关主题here的更多信息。