目标[Illuminate \ Contracts \ Debug \ ExceptionHandler]不可实例化

时间:2016-12-23 16:15:15

标签: php laravel laravel-5 eloquent slim

我正在将Eloquent与Slim Framework结合使用来构建一个小型API。由于某种原因,我收到此错误,我找不到问题:

Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable.

数据库设置正确。在服务器上的暂存环境中抛出此错误。在本地,使用MySQL 5.6.29,它可以很好地工作。虽然我不认为它与MySQL 5.7有关,因为我在同一台服务器上运行了另一个使用相同版本的Eloquent的应用程序。

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

我正在使用:

  • 雄辩版本: 5.3.23
  • PHP版本: 5.6.28
  • 数据库驱动程序&版本:MySQL 5.7.16

回溯

/path/to/app/shared/vendor/illuminate/container/Container.php:644 
Illuminate\Container\Container -> make
/path/to/app/shared/vendor/illuminate/database/Connectors/ConnectionFactory.php:130 
call_user_func 
/path/to/app/shared/vendor/illuminate/database/Connection.php:964 
Illuminate\Database\Connection -> getPdo 
/path/to/app/shared/vendor/illuminate/database/Connection.php:832 
Illuminate\Database\Connection -> reconnectIfMissingConnection 
/path/to/app/shared/vendor/illuminate/database/Connection.php:717 
Illuminate\Database\Connection -> run 
/path/to/app/shared/vendor/illuminate/database/Connection.php:350 
Illuminate\Database\Connection -> select 
/path/to/app/shared/vendor/illuminate/database/Query/Builder.php:1648 
Illuminate\Database\Query\Builder -> runSelect 
/path/to/app/shared/vendor/illuminate/database/Query/Builder.php:1634 
Illuminate\Database\Query\Builder -> get 
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:632 
Illuminate\Database\Eloquent\Builder -> getModels 
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:327 
Illuminate\Database\Eloquent\Builder -> get 
/path/to/app/shared/vendor/illuminate/database/Eloquent/Builder.php:297 
Illuminate\Database\Eloquent\Builder -> first 
...

我怀疑它与未注册的错误处理程序有关:

github link

但是如何在不使用Laravel时注册自定义错误处理程序?

更新 @patricus的解决方案可以实现如下:

// bootstrap Eloquent ORM
$container = new Container();
$container->singleton(
  'Illuminate\Contracts\Debug\ExceptionHandler'
, 'My\Custom\DatabaseExceptionHandler'
);

$factory    = new ConnectionFactory( $container );
$connection = $factory->make([
  'driver'    => 'mysql'
, 'host'      => App::config( 'database_host' )
, 'database'  => App::config( 'database_name' )
, 'username'  => App::config( 'database_user' )
, 'password'  => App::config( 'database_password' )
, 'charset'   => App::config( 'database_charset' )
, 'collation' => App::config( 'database_collate' )
, 'prefix'    => ''
]);

$resolver = new ConnectionResolver();
$resolver->addConnection( 'default', $connection );
$resolver->setDefaultConnection( 'default' );

// initialize connection
Model::setConnectionResolver( $resolver );

2 个答案:

答案 0 :(得分:3)

您应该能够访问容器以通过胶囊注册您自己的异常处理程序。

$capsule = new \Illuminate\Database\Capsule\Manager();

$capsule->getContainer()->singleton(
    \Illuminate\Contracts\Debug\ExceptionHandler::class,
    \Your\ExceptionHandler\Implementation::class
);

如果你想要一个真正快速,非常愚蠢的异常处理程序,你可以使用它:

class DumbExceptionHandler implements \Illuminate\Contracts\Debug\ExceptionHandler
{
    public function report(Exception $e)
    {
        //
    }

    public function render($request, Exception $e)
    {
        throw $e;
    }

    public function renderForConsole($output, Exception $e)
    {
        throw $e;
    }
}

注意:未经测试。我相信这应该可以看到代码。

答案 1 :(得分:0)

请确保您的xampp服务器和数据库服务器可以正常运行。