Search code examples
phplaravelcomposer-phplumenautoloader

Composer class not found even if it exists


I'm developing a Laravel package but I have a problem with composer autoloading. My package has 2 folders under src folder. One of them is named Laravel and the other one is Telegram. Here is the package structure:

./packages
.../typhoon
...../src
......./Laravel
........./Providers
............LumenServiceProvider.php
............LaravelServiceProvider.php
......./Telegram
..........Api.php
.....composer.json

This package is developed under SaliBhdr/Typhoon namespace.

I have added the packages/typhoon/src directory in Laravel's composer file like so:

    "autoload": {
        "psr-4": {
            "App\\": "app/",
            "SaliBhdr\\Typhoon\\" : "packages/typhoon/src/"
        }
    },

And add the src/ address in package composer.json file like so:

    "autoload": {
        "psr-4": {
            "SaliBhdr\\Typhoon\\": "src/"
        }
    },

Here is the strange behavior begins. When I execute the php artisan serve command Laravel throws an error that says :

 Class 'Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider' not found 

And if I check if the class exists with class_exists('Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider') function it returns false. But if I check if Salibhdr\Typhoon\Telegram\Api exists it returns true.

I checked the autoload_classmap file and notice that the composer includes all the classes under Telegram subfolder but not Laravel subfolder.

Why composer acts weird like this? why did it include one subfolder and not the other? It is something that I do every day and never seen anything like this.

I desperately need help. Any help would be appreciated


Solution

  • You are trying to initialize Salibhdr\Typhoon\Laravel\Providers\LumenServiceProvider but in your composer it's "SaliBhdr\\Typhoon\\": "src/".

    Notice the capital B in your composer. PHP classes are case sensitive so you have to make sure it's either both lowercase or both uppercase.

    Also make sure to run composer dumpautoload if you modify composer.json.