user936965
user936965

Reputation:

class not found with composer autoloader

I have this in my composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "php": ">=7.0.0",
        "alchemy/zippy": "^0.4.8",
        "barryvdh/laravel-debugbar": "^3.1",
        "fideloper/proxy": "~3.3",
        "graham-campbell/exceptions": "^10.0",
        "intervention/image": "^2.4",
        "intervention/imagecache": "^2.3",
        "laravel/framework": "5.5.*",
        "laravel/tinker": "~1.0",
        "laravelcollective/html": "^5.5",
        "symfony/dom-crawler": "^3.3"
    },
    "files": [
        "vendor/redbutton/text-image-alpha/vendor/autoload.php"
    ],
    "require-dev": {
        "filp/whoops": "^2.1",
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~6.0"
    },
    "autoload": {
        "classmap": [
            "database/seeds",
            "database/factories"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "extra": {
        "laravel": {
            "dont-discover": [
            ]
        }
    },
    "scripts": {
        "post-root-package-install": [
            "@php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "@php artisan key:generate"
        ],
        "post-autoload-dump": [
            "Illuminate\\Foundation\\ComposerScripts::postAutoloadDump",
            "@php artisan package:discover"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

The php files for my package are in a laravel installation: /vendor/redbutton/text-image-alpha/src/

When I try to call a file I get the message:

Class 'Class 'RedButton\TextImageAlpha\TextImageAlpha' not found' not found

I use this to try and call the class:

$image     = new \RedButton\TextImageAlpha\TextImageAlpha( 'some string' );

The file in /vendor/redbutton/text-image-alpha/src/TextImageAlpha.php looks like this:

<?php
namespace RedButton\TextImageAlpha;

use RedButton\TextImageAlpha\Exceptions;
use RedButton\Tools\Objects;


/**
 * TextImageAlpha class convert a text to image.
 *
 * @author Tomas Rathouz <trathouz at gmail.com>
 */
class TextImageAlpha
{
   // lots of code
}

This is my first composer package and I don't really have an idea of what is going wrong here. Could someone please explain to me what I'm doing wrong?

Upvotes: 2

Views: 3212

Answers (1)

Tomas Votruba
Tomas Votruba

Reputation: 24298

Good news. I've looked at your package at Bitbucket and you're doing it right in its composer.json:


Drop unnecessary code from composer.json

I've noticed in pasted composer.json there is manual adding of some vendor file.

"files": [
    "vendor/redbutton/text-image-alpha/vendor/autoload.php"
],

/vendor nested in /vendor doesn't make sense as it might duplicate autoloading and break it.

Also I don't see redbutton/text-image-alpha in require section, where it should be.


How to add package the right way?

To install your package just call composer:

composer require redbutton/text-image-alpha

And that's it.

It should appear in require section in composer.json. Composer will autoload it by rules in composer.json of the package - here.


Test

I've tried following code and class is found correctly:

<?php 

require __DIR__ . '/vendor/autoload.php';

$image = new \RedButton\TextImageAlpha\TextImageAlpha('some string');
var_dump($image);

Having composer.json:

{
    "require": {
        "redbutton/text-image-alpha": "^1.0"
    }
}

Upvotes: 1

Related Questions