I used to have a modified version of Dingo service provider (I'm using Laravel 4.2) that works beautifully. However after a while (several composer update) I can't seem to get it working anymore. It is simply not registered. I even tried to put some var_dump
or echo
in the __construct
method and it has nothing
The only difference I make is for it to capture more exception types
My app.php
'providers' => array(
....
'MyMod\Api\Provider\MainServiceProvider',
'Dingo\Api\Provider\ApiServiceProvider',
),
My service provider
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;
}
My handler
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;
}
}
Can someone point me to a correct direction about what might go wrong?
Edit: I'm more and more sure it has something to do with composer and psr-4
What do you think about my composer.json below, it was working
"autoload": {
"classmap": [
"app/commands",
"app/controllers",
"app/models",
"app/database/migrations",
"app/database/seeds",
"app/tests/TestCase.php",
"app/transformer",
"app/customPackages",
"app/routes"
],
},
My service provider and handler is in app/customPackages folder