dpa89292 2017-02-23 13:04
浏览 227
已采纳

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

Here is my main Slim app instantiation code:

$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' ] );

You can see that the last line is supposed to unset the errorHandler. Yet, in a controller for example I declare a function with:

public function index( $request, Response $response )

I forgot to set the 'Response' use statement, so it created an error (easily fixed but that's not the point). But this error was handled by slim. Why is that ? Isn't it supposed not to handle errors from the unset statement ?

UPDATE: error:

Details

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条回答 默认 最新

  • duanqiao1961 2017-02-23 14:42
    关注

    Argument 2 passed to Rib\Src\Controllers\HomeController::index() must be an instance of Rib\Src\Controllers\Response, instance of Slim\Http\Response given

    This is a namespace declaration error.Answer

    In your controller file (HomeController) at the top add this:

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

    UPDATE

    Sorry, I misread the question. So, you want to know why the error was handled by Slim.

    This is because the error is a PHP Runtime error. And Slim has special handler for that. In container it is stored under the phpErrorHandler key. By default it returns a 500 response code, and, of course, you can define your own error handler.

    Therefore, to disable Slim's handling of PHP errors, unset $container['phpErrorHandler'].

    Important note: This handler is PHP7+ only, since PHP Error class was introduced in PHP7.0.

    You can read more about subject here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论
查看更多回答(1条)

报告相同问题?

悬赏问题

  • ¥15 树莓派与pix飞控通信
  • ¥15 自动转发微信群信息到另外一个微信群
  • ¥15 outlook无法配置成功
  • ¥30 这是哪个作者做的宝宝起名网站
  • ¥60 版本过低apk如何修改可以兼容新的安卓系统
  • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
  • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
  • ¥50 有数据,怎么用matlab求全要素生产率
  • ¥15 TI的insta-spin例程
  • ¥15 完成下列问题完成下列问题