0

我曾经有一个修改版的 Dingo 服务提供者(我使用的是 Laravel 4.2),效果很好。但是过了一段时间(几个作曲家更新)我似乎无法让它工作了。它根本没有注册。我什至尝试在方法中添加一些var_dump或,但它什么都没有echo__construct

我做的唯一不同是让它捕获更多的异常类型

我的 app.php

'providers' => array(       
    ....
    'MyMod\Api\Provider\MainServiceProvider',
    'Dingo\Api\Provider\ApiServiceProvider',
),

我的服务商

namespace MyMod\Api\Provider;

use Illuminate\Support\ServiceProvider;
use MyMod\Api\Event\ExceptionHandler;

class MainServiceProvider extends ServiceProvider
{

/**
 * Boot the service provider.
 * @return void
 */
public function boot()
{
    $this->app['events']->listen('router.exception', 'MyMod\Api\Event\ExceptionHandler');
}

/**
 * Register bindings for the service provider.
 * @return void
 */
public function register()
{
    $this->app->bind('MyMod\Api\Event\ExceptionHandler', function ($app) {
        return new ExceptionHandler($app['api.exception'], $app['config']->get('api::debug'));
    });

}

/**
 * Indicates if loading of the provider is deferred.
 * @var bool
 */
protected $defer = true;

}

我的处理程序

namespace MyMod\Api\Event;

use Exception;
use MyMod\Api\Exception\ApiException;
use Dingo\Api\Http\Response;
use Dingo\Api\Exception\Handler;
use Symfony\Component\HttpKernel\Exception\HttpExceptionInterface;

class ExceptionHandler
{
/**
 * API exception handler instance.
 * @var \Dingo\Api\Exception\Handler
 */
protected $handler;

/**
 * Indicates if debug mode is enabled.
 * @var bool
 */
protected $debug;

/**
 * Create a new exception handler instance.
 * @param \Dingo\Api\Exception\Handler $handler
 * @param bool                         $debug
 */
public function __construct(Handler $handler, $debug = false)
{
    $this->handler = $handler;
    $this->debug = $debug;
}

/**
 * Handle an exception thrown during dispatching of an API request.
 * @param \Exception $exception
 * @throws \Exception
 * @return \Dingo\Api\Http\Response
 */
public function handle(Exception $exception)
{
    if ($this->handler->willHandle($exception)) {
        $response = $this->handler->handle($exception);

        return Response::makeFromExisting($response);
    } elseif (! $exception instanceof HttpExceptionInterface) {
        throw $exception;
    }

    if (! $message = $exception->getMessage()) {
        $message = sprintf('%d %s', $exception->getStatusCode(), Response::$statusTexts[$exception->getStatusCode()]);
    }

    $response = ['message' => $message, 'status_code' => $exception->getStatusCode()];

    if ($exception instanceof ApiException && $exception->hasErrors()) {
        $response['errors'] = $exception->getErrors();
    }

    if ($code = $exception->getCode()) {
        $response['code'] = $code;
    }

    if ($this->debug) {
        $response['debug'] = [
            'line' => $exception->getLine(),
            'file' => $exception->getFile(),
            'class' => get_class($exception),
            'trace' => $exception->getTraceAsString(),
        ];
    }

    return new Response($response, $exception->getStatusCode(), $exception->getHeaders());
}

/**
 * Enable or disable debug mode.
 * @param bool $debug
 * @return void
 */
public function setDebug($debug)
{
    $this->debug = $debug;
}
}

有人可以为我指出可能出错的正确方向吗?

编辑:我越来越确定它与作曲家和 psr-4 有关

您如何看待我下面的 composer.json,它正在工作

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "app/transformer",
        "app/customPackages",
        "app/routes"
    ],        
},

我的服务提供者和处理程序位于 app/customPackages 文件夹中

4

0 回答 0