Target [Illuminate\Contracts\Debug\ExceptionHandler] is not instantiable - php

I'm using Eloquent in combination with Slim Framework to build a small 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. 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.
Can anyone point me in the right direction?
I'm using:
Eloquent Version: 5.3.23
PHP Version: 5.6.28
Database Driver & Version: MySQL 5.7.16
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:
github link
But how can I register the custom error handler when not using Laravel?
The solution of #patricus can be implemented as follows:
// 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.

In my case it was permissions issue of system user who executes php command.

Please make sure that your xampp server and the database server is working and running.

Related

Symfony 2 Doctrine 2 EntityManager config

i'm trying to get my Doctrine CommandLine Tool working in Symfony 2 project on Windows 7 and I keep getting the same error message in console:
Fatal error: Call to protected Doctrine\ORM\EntityManager::__construct()
from invalid context in C:\wamp\www\firstSymfonyApp\cli-config.php on line 9
Call Stack:
0.0010 239440 1. {main}() C:\wamp\www\firstSymfonyApp\vendor\doctrine\orm\bin\doctrine.php:0
0.0090 621376 2. require('C:\wamp\www\firstSymfonyApp\cli-config.php') C:\wamp\www\firstSymfonyApp\vendor\doctrine\orm\bin\doctrine.php:48
Code of my cli-config.php file:
<?php
use Doctrine\ORM\Tools\Console\ConsoleRunner;
require_once 'app/bootstrap.php.cache';
$em = new \Doctrine\ORM\EntityManager();
return ConsoleRunner::createHelperSet($em);
Until today, I was only using doctrine on Linux where the installation was much more simple, please help me work this out.
Error message is very clear. EntityManager::__construct is protected method therefore you can't use it outside of the class.
Check out EntityManager::create.
Check this link for more information about how to start with Doctrine 2.
This is probably the snippet which should be important to you right now:
<?php
// bootstrap.php
require_once "vendor/autoload.php";
use Doctrine\ORM\Tools\Setup;
use Doctrine\ORM\EntityManager;
$paths = array("/path/to/entity-files");
$isDevMode = false;
// the connection configuration
$dbParams = array(
'driver' => 'pdo_mysql',
'user' => 'root',
'password' => '',
'dbname' => 'foo',
);
$config = Setup::createAnnotationMetadataConfiguration($paths, $isDevMode);
$entityManager = EntityManager::create($dbParams, $config);

Standalone Laravel queue does not work

I am trying to use Laravel queue package outside of Laravel with the database driver, but I got an error with resolving namespaces.
Value of $concrete is 'db'.
$reflector = new ReflectionClass($concrete);
Exception message:
Fatal error: Uncaught exception 'ReflectionException' with message 'Class db does not exist' in ..vendor\illuminate\container\Container.php on line 736
ReflectionException: Class db does not exist in ..vendor\illuminate\container\Container.php on line 736
I am using the illuminate DB package also, and it is working fine, but this one throws exception.
Need resolve DB connection manually and add this connection to Queue manager
Grab mysql connection from DB manager:
$connection = Capsule::schema()->getConnection();
$container = new Container();
Grab Queue manager instance:
$manager = $queue->getQueueManager();
Resolve connection:
$resolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]);
Add connection to Queue manager:
$manager->addConnector('database', function () use ($resolver) {
return new DatabaseConnector($resolver);
});
Done!
Also need add Illuminate Encryptionn package
Complete code:
<?php
/**
* Created by PhpStorm.
* User: Dmitry
* Date: 05.08.2015
* Time: 19:32
*/
require_once "../vendor/autoload.php";
require_once '../db/db_inc.php'; // DB Capsule config
use Illuminate\Container\Container;
use Illuminate\Queue\Capsule\Manager as Queue;
use Illuminate\Database\Capsule\Manager as Capsule;
use Illuminate\Queue\Connectors\DatabaseConnector;
$connection = Capsule::schema()->getConnection();
$queue = new Queue();
$queue->addConnection([
'driver' => 'database',
'table' => 'jobs', // Required for database connection
'connection' => 'default',
'host' => 'localhost',
'queue' => 'default',
]);
$queue->getContainer()->bind('encrypter', function() {
return new \Illuminate\Encryption\Encrypter('1111111111111111');
});
$queue->getContainer()->bind('request', function() {
return new \Illuminate\Http\Request();
});
$manager = $queue->getQueueManager();
$resolver = new \Illuminate\Database\ConnectionResolver(['default' => $connection]);
$manager->addConnector('database', function () use ($resolver) {
return new DatabaseConnector($resolver);
});
$queue->setAsGlobal();
Queue::push('SomeJobClass', ['parameters']);

Add Doctrine DBAL into own php project

Trying to add Doctrine DBAL into my own project to use it to access my db etc. I don't have composer and i never used it. This is what i am trying to do according to the docu:
use Doctrine\Common\ClassLoader;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
require_once "doctrine/Common/ClassLoader.php";
$classLoader = new ClassLoader('Doctrine', 'doctrine');
$classLoader->register();
$config = new Configuration();
$connectionParams = array(
'dbname' => 'mydb',
'user' => 'root',
'password' => "",
'host' => 'localhost',
'driver' => 'pdo_mysql',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
}
This is taken from here:
-http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/configuration.html
and:
- http://docs.doctrine-project.org/projects/doctrine-dbal/en/latest/reference/introduction.html
I have the Common and DBAL folder added into my project
My folder structure looks like this:
root
doctrine
DBAL
Common
php stuff
index.php (where connection.php) is executed
So what happens is that i either get "Cannot find class XY" or something similar, based upon what i change on the code. I never am able to execute it as it should following the tutorial.
What am i doing wrong here?
I just want to have the connection object, where i can start doing my stuff like useing the query builder etc...
I am completely lost here...
UPDATE: Installed composer as requested and have this Code now:
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Which is the 2nd code example in my 1st link. Tells me " Class 'Doctrine\DBAL\Configuration' not found ". Funny thing is, that IntelliJ can perfectly autocomplete the path (suggests me Configuration when finishing DBAL in the path) but PHP doesn't find it. If i remove the new Configuration PHP just tells me, that it doesn't find the DriverManager...
I installed it correctly via composer though, at least composer tells me it is installed correctly now (Where does composer save the libs?)
You now need to require composers autoload file.
require __DIR__.'/vendor/autoload.php';
use Doctrine\DBAL\Configuration;
class Connection
{
var $connection;
//Constructor
public function __construct()
{
$config = new Configuration();
$connectionParams = array(
'url' => 'mysql://root:secret#localhost/mydb',
);
$this->connection = \Doctrine\DBAL\DriverManager::getConnection($connectionParams, $config);
}
Please note, depending on your directory structure, the autoload file might be somewhere else, but usually this should work.
Pay attention to the use of namespaces: if the Doctrine namespace for its loader is Doctrine\Common\ClassLoader, you have to put the files inside the Doctrine\Common folder ("Doctrine" with a capital "D").
See the code snippet shown inside the Introduction chapter of the Doctrine DBAL documentations.

Phalcon's universal class loader can't find my class

As a total php and phalcon newbie, I'm trying to use the recommended universal class loader using this code:
$loader = new \Phalcon\Loader();
// Register some directories
$loader->registerDirs(
array(
"php/assistants/"
)
);
// register autoloader
$loader->register();
$test = new dbAssistant();
as I understand I have to reffer to the php file as a class what I have inside of php/assistants/dbAssistant.php is the following code, trying to connect to a database:
<?php
function connect() {
$connection = new Phalcon\Db\Adapter\Pdo\Mysql(array(
'host' => 'localhost',
'username' => 'root',
'password' => 'tt',
'dbname' => 'testdb',
'port' => '3306'
));
echo 'Connected!!!';
}
again what I understand is that I have to refer to dbAssistant.php as a class and that's why I'm using $test = new dbAssistant();, but it gives me the following error:
Fatal error: Class 'dbAssistant' not found in /var/www/html/test/test.php on line 18
I know that it seeme normal, but the strange thing is that if I remove the connect() function and place the code out of it, I can see the Connected!!! echo, but it is followed by the same(above) error. I know that I'm missing something really small here, but as a complete php newbie, I really can't spot the problem.
Can you give me a push?
php/assistants/dbAssistant.php is not a class but a plain Php file. There should be a class in there with the name dbAssistant.
class dbConnect {
public function connect() {
///Do your stuff
}
}

Error when Call to a member function connection() on a non-object while using Illuminate Database

I have installed Sentry with Composer, thus it has installed Illuminate Database too. The installation was successful, I have done everything in the Sentry documentation. I'm trying to add a user to the database with a simple code. However it gives me this error message:
Fatal error: Call to a member function connection() on a non-object in C:\Program Files\EasyPHP-DevServer-14.1VC9\data\localweb\ilhan\vendor\illuminate\database\Illuminate\Database\Eloquent\Model.php on line 2472
My code is as follows:
<?php
include_once "vendor/autoload.php";
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
'driver' => 'mysql',
'host' => 'localhost',
'database' => 'ilhantestdb',
'username' => 'root',
'password' => '',
'charset' => 'utf8',
'collation' => 'utf8_unicode_ci',
]);
use Cartalyst\Sentry\Sentry as Sentry;
try
{
$user = new Sentry;
// Create the user
$user->createUser(array(
'email' => 'john.doe#example.com',
'password' => 'test',
'activated' => true,
));
// Find the group using the group id
$adminGroup = Sentry::findGroupById(1);
// Assign the group to the user
$user->addGroup($adminGroup);
}
catch (Cartalyst\Sentry\Users\LoginRequiredException $e)
{
echo 'Login field is required.';
}
catch (Cartalyst\Sentry\Users\PasswordRequiredException $e)
{
echo 'Password field is required.';
}
catch (Cartalyst\Sentry\Users\UserExistsException $e)
{
echo 'User with this login already exists.';
}
catch (Cartalyst\Sentry\Groups\GroupNotFoundException $e)
{
echo 'Group was not found.';
}
Even I don't know how to debug this. Also, I thought that since Illuminate comes with Sentry, Sentry should be coded how to handle Illuminate thus I don't need much configuration. The documentation is poor and I was unable to find what to do with this error.
You must also boot the ORM. Documentation is not really clear about this.
use Illuminate\Database\Capsule\Manager as Capsule;
$capsule = new Capsule;
$capsule->addConnection([
...
]);
$capsule->bootEloquent();
Try setting Capsule as a global. I can't speak to Sentry, but I'm trying to use Capsule on a project, myself, and am running into this very same issue.
Take a look here: https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L113 You'll see that a lot of the convenience functions in there depend on having the static::$instance variable set, which gets set on https://github.com/illuminate/database/blob/master/Capsule/Manager.php#L192.
In my case, I'm trying to use $capsule without setting it as a global. What I ended needing to do was to write my query like so $capsule->getConnection()->table('foo')->get(). In your case, I think Sentry is attempting to access Eloquent and Capsule through static class methods.
tl;dr Run $capsule->setAsGlobal();

Categories