teamf teamf - 1 month ago 12
PHP Question

Fatal error: Class ... not found in ... /ProviderRepository.php on line 146

This sounds like a trivial mistake, but I've been unable to find the solution for hours.

I tried to use https://github.com/michaelbonds/laravel-db2 in my Laravel project, and I added it using composer.

Here's my composer.json.

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"type": "project",
"require": {
"php": ">=5.5.9",
"laravel/framework": "5.1.*",
"michaelb/laravel-db2": "~2.0"
},
"require-dev": {
"fzaninotto/faker": "~1.4",
"mockery/mockery": "0.9.*",
"phpunit/phpunit": "~4.0",
"phpspec/phpspec": "~2.1"
},
"autoload": {
"classmap": [
"database"
],
"psr-4": {
"App\\": "app/"
}
},
"autoload-dev": {
"classmap": [
"tests/TestCase.php"
]
},
"scripts": {
"post-install-cmd": [
"php artisan clear-compiled",
"php artisan optimize"
],
"pre-update-cmd": [
"php artisan clear-compiled"
],
"post-update-cmd": [
"php artisan optimize"
],
"post-root-package-install": [
"php -r \"copy('.env.example', '.env');\""
],
"post-create-project-cmd": [
"php artisan key:generate"
]
},
"config": {
"preferred-install": "dist"
}
}


Here's the error:

PHP Fatal error: Class 'MichaelB\Database\DB2\DB2ServiceProvider' not found in /var/www/html/mysjsu/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 146


and

[Symfony\Component\Debug\Exception\FatalErrorException]
Class 'MichaelB\Database\DB2\DB2ServiceProvider' not found


I found a similar question before on StackOverflow, and somebody suggested that the order matters. So, here's what I did in the following order:


  1. add
    "michaelb/laravel-db2": "~2.0"
    to
    composer.json

  2. run
    php ../composer.phar update

  3. add
    MichaelB\Database\DB2\DB2ServiceProvider::class
    to the
    providers
    arary in
    config/app.php

  4. run
    php artisan migrate



Any idea on what might've been the problem?

Answer

This package is a fork of cooperl/laravel-db2. The documentation is inaccurate.

He changed the namespace to MichaelB but never tagged this as a stable version, so when you composer require his ~2.0, packagist gives you an older version with the wrong namespace.

So when you run composer update, it runs some post-cmd scripts such as php artisan optimize which tries to pull from the config file but fails because that class doesn't exist.

Here's how to fix it:

  1. Change composer.json to this version:

    "michaelb/laravel-db2": "dev-master"
    
  2. Run composer update --no-scripts

Then you should be good.

Comments