Slim 3中的自定义错误处理程序无法正常工作

问题描述 投票:0回答:1

我想覆盖Slim 3中的默认错误处理程序,并使用JSON而不是默认的HTML页面进行响应。但我不能让它工作,我的自定义处理程序完全被忽略,我无法弄清楚为什么。

我的项目结构如下所示:

api/
  public/
    index.php
  src/
    config/
      database.php
      handlers.php
      settings.php
    models/
      product.php
    routes/
      products.php

我的index.php看起来像这样:

<?php

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

require __DIR__ . '/../vendor/autoload.php';
require __DIR__ . '/../src/config/database.php';

// Instantiate the app
$settings = require __DIR__ . '/../src/config/settings.php';
$app = new \Slim\App(['settings' => $settings]);

// Set up handlers
$container = $app->getContainer();
require __DIR__ . "/../src/config/handlers.php";

// Register routes
require __DIR__ . '/../src/routes/products.php';

// Run app
$app->run();

我的handlers.php文件包含所有自定义错误处理程序,如下所示:

<?php 

/**
 * Custom global error handler.
 */
$container['errorHandler'] = function($container) {
  return function ($request, $response, $exception) use ($container) {
      return $response->withStatus(500)
          ->withHeader('Content-Type', 'application/json')
          ->write(json_encode(array(
              'error' => 'INTERNAL_ERROR',
              'error_message' => 'Something went wrong internally.',
              'status_code' => '500',
              'trace' => $exception.getTraceAsString()
            ), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));    
  };
};

/**
* Custom global PHP error handler.
*/
$container['phpErrorHandler'] = function($container) {
  return $container['errorHandler'];
};

/**
* Custom 404 Not Found error handler.
*/
$container['notFoundHandler'] = function($container) {
  return function ($request, $response) use ($container) {
      return $response->withStatus(404)
          ->withHeader('Content-Type', 'application/json')
          ->write(json_encode(array(
              'error' => 'NOT_FOUND',
              'error_message' => 'Endpoint was not found. Check API documentation for valid endpoints.',
              'status_code' => '404',
            ), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));    
  };
};

/**
* Custom 405 Not Allowed error handler.
*/
$container['notAllowedHandler'] = function($container) {
  return function ($request, $response, $methods) use ($container) {
      return $response->withStatus(405)
          ->withHeader('Allow', implode(', ', $methods))
          ->withHeader('Content-Type', 'application/json')
          ->write(json_encode(array(
              'error' => 'NOT_ALLOWED',
              'error_message' => 'HTTP request method is not allowed. Method must be of: ' . implode(', ', $methods),
              'status_code' => '405',
            ), JSON_UNESCAPED_SLASHES | JSON_PRETTY_PRINT));    
  };
};

到目前为止我尝试了什么:

  • 在创建$app并将$container加载到其中之前添加所有处理程序。
  • 在创建自定义处理程序之前取消设置,如下所示:unset($app->getContainer()['notFoundHandler']);

我无法解决出错的原因以及为什么我在抛出错误时仍然会获得默认的HTML视图。

php slim slim-3
1个回答
0
投票

感谢@Nima,我发现我在$app = new \Slim\App;文件中意外地创建了一个新的products.php实例。删除它使它工作。

© www.soinside.com 2019 - 2024. All rights reserved.