PHPFixing
  • Privacy Policy
  • TOS
  • Ask Question
  • Contact Us
  • Home
  • PHP
  • Programming
  • SQL Injection
  • Web3.0

Sunday, January 16, 2022

[FIXED] Composer class not found even if it exists

 January 16, 2022     autoloader, composer-php, laravel, lumen, php     No comments   

Issue

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.



Answered By - Chin Leung
  • Share This:  
  •  Facebook
  •  Twitter
  •  Stumble
  •  Digg
Newer Post Older Post Home

0 Comments:

Post a Comment

Note: Only a member of this blog may post a comment.

Total Pageviews

Featured Post

Why Learn PHP Programming

Why Learn PHP Programming A widely-used open source scripting language PHP is one of the most popular programming languages in the world. It...

Subscribe To

Posts
Atom
Posts
Comments
Atom
Comments

Copyright © PHPFixing