1
votes

I created a package for Laravel 4 that worked properly when used in development in workbench but when I install it with Composer it keeps returning me the error Class 'Myvendor\Mypackage\MypackageServiceProvider' not found.

There is a particularity with my package which is that the name of my classes sources are different from the name of my package. Usually they are the same.

vendor/
    Houle/
        laravel-dynamite/
            src/
                Fhoule/
                    Dynamite/
                        DynamiteServiceProvider.php

I know that it can work because Laravel work's this way too.

vendor/
    laravel/
        framework/
            src/
                Illuminate/

And the property PSR-0 of my package composer.json seems to be properly configured:

"name": "Houle/laravel-dynamite",
...
"require": {
    "php": ">=5.3.0",
    "illuminate/support": "4.0.x"
},
"autoload": {
    "classmap": [
        "src/migrations",
        "src/controllers",
        "src/models"
    ],
    "psr-0": {
        "Fhoule\\Dynamite": "src/"
    }
},
...

How I created my package:

  • Created the package with Artisan.
  • Maked it work properly in workbench directory
  • Pushed to private Bitbucket repo
  • Installed new instance of Laravel
  • Changed composer.json configuration to install my package (from private repository)

    "name": "laravel/laravel",
    ...
    "require": {
        "laravel/framework": "4.0.*",
        "Houle/laravel-dynamite": "2.0.1"
    },  
    "repositories": [{
        "type": "package",
        "package": {
            "name": "Houle/laravel-dynamite",
            "version": "2.0.1",
            "source": {
                "url": "[email protected]:Houle/laravel-dynamite.git",
                "type": "git",              
                "reference": "v2.0.1"
            }
        }
    }],
    ...
    
  • Added my package Service Provider to app/config/app.php:

    'providers' => array(
        'Fhoule\Dynamite\DynamiteServiceProvider',
    )
    

That's where my application return the error Class 'Fhoule\Dynamite\DynamiteServiceProvider' not found.

What could be my issue?

2
Did you run php artisan dump-autoload ?Hao Luo
It's not necessary after executing composer install or update, but yes I tried it.FR6
Ok, but actually, it is necessary to run artisan's dump-autoload if you are using workbench as it does more than the composer dump-autoload.Hao Luo

2 Answers

9
votes

I found my problem, it hadn't nothing to do with the way I named my vendor, package and classes.

It was that in my composer.json (root of the project), I set my repository type to package but like the Composer documentation states the type package is for packages that don't support Composer. That's why Composer wasn't updating my autoload_classmap.php file.

So if you want to use a private repository (like at Bitbucket or GitHub) you need to set the type of the repository to git:

{
    "name": "laravel/laravel",
    ...
    "require": {
        "laravel/framework": "4.0.*",
        "houle/laravel-dynamite": "dev-master"
    },  
    "repositories": [{
        "type": "git",
        "url": "[email protected]:Houle/laravel-dynamite.git"       
    }],
    ...
}

Hope it helps someone.

0
votes

The composer.json in your package (Package found in BitBucket) needs to specify the PSR-0 Autoloading component, rather than the composer.json file in your top-level Laravel project.

Can you show us the composer.json file for your repo in the private repository?