Laravel服务提供商

时间:2015-07-21 23:37:48

标签: php laravel composer-php service-provider dingo-api

我曾经有一个修改版的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;
}
}

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

编辑:我越来越确定它与composer和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文件夹

0 个答案:

没有答案
相关问题