Saturday, January 1, 2022

[FIXED] Class not found using custom composer package

Issue

I have created a custom comoposer package and I want to use it on my project with this composer.json:

{
    "name": "papillon/test",
    "type": "library",
    "version": "dev-master",
    "require": {
        "php": "^7.1.11"
    },
    "autoload": {
        "psr-4": {
            "Papillon\\Fountaine\\Eau\\": "src/Papillon/Fountaine/Eau/"
        }
    }  
}

I compress it in zip. In the main project, I add a folder called repo, where I add de composer package zip. Then, I modify the composer.json of the main project like this:

{
    "repositories": [
        {
            "type": "artifact",
            "url": "var/main/repo"
        }
    ],
    "require": {
        "papillon/test": "dev-master"
    }
    
}

I execute composer update and the pakage is added to vendor folder; all seems to be going well... but if I want to test the package from the main project with this script:

<?php

require (__DIR__ . '/vendor/autoload.php');
use Papillon\Fountaine\Eau\FlowerClass;

echo FlowerClass::bloom();

It returns: PHP Fatal error: Uncaught Error: Class 'Papillon\Fountaine\Eau\FlowerClass' not found in .../test_package.php:6 Stack trace: #0 {main} thrown in .../test_package.php on line 6

I think that the package may not be recognized by the main project; maybe the package was improperly installed in the main project?


Solution

Debugging autoload can be very useful to catch errors. Take care with the route paths, the autoload tryed to find the classes files in a path with a lowercase folder when in the package composer.json the route was definded with that folder uppercase.



Answered By - aL_peLi

No comments:

Post a Comment

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