[英]Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable
I'm using Eloquent in combination with Slim Framework to build a small API.我将 Eloquent 与 Slim Framework 结合使用来构建一个小型 API。 For some reason, I am getting this error, and I can't locate the problem:出于某种原因,我收到此错误,但找不到问题所在:
Target [Illuminate\\Contracts\\Debug\\ExceptionHandler] is not instantiable.
Database settings are correct.数据库设置正确。 This error is thrown in my staging environment on the server.此错误是在服务器上的暂存环境中引发的。 Locally, with MySQL 5.6.29, it works perfectly.在本地,使用 MySQL 5.6.29,它可以完美运行。 Although I don't think it's related to MySQL 5.7 because I have another app running on the same server with the same version of Eloquent.虽然我认为它与 MySQL 5.7 无关,因为我在同一台服务器上运行了另一个应用程序,使用相同版本的 Eloquent。
Can anyone point me in the right direction?任何人都可以指出我正确的方向吗?
I'm using:我正在使用:
Backtrace回溯
/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
...
I'm suspecting it has something to do with an unregistered error handler:我怀疑它与未注册的错误处理程序有关:
But how can I register the custom error handler when not using Laravel?但是在不使用 Laravel 时如何注册自定义错误处理程序?
UPDATE The solution of @patricus can be implemented as follows:更新@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 );
You should be able to access the container to register your own exception handler through the capsule.您应该能够访问容器以通过胶囊注册您自己的异常处理程序。
$capsule = new \Illuminate\Database\Capsule\Manager();
$capsule->getContainer()->singleton(
\Illuminate\Contracts\Debug\ExceptionHandler::class,
\Your\ExceptionHandler\Implementation::class
);
If you want a real quick, very dumb, exception handler, you can use this:如果你想要一个真正快速、非常愚蠢的异常处理程序,你可以使用这个:
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;
}
}
NB: untested.注意:未经测试。 I believe this should work looking at the code.我相信这应该可以查看代码。
请确保您的 xampp 服务器和数据库服务器正在运行。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.