0

I made a laravel package with this composer.json:

{
    "name": "calcanotica/file-storage",
    "type": "library",
    "description": "A file storage module for laravel.",
    "homepage": "<<gitlab url>>",
    "authors": [
        {
            <<authors>>
        }
    ],
    "autoload": {
        "psr-4": { "Calcanotica": "src" }
    },
    "require": {
        "php": "^5.5.9 || ^7.0",
        "illuminate/contracts": "5.1.* || 5.2.* || 5.3.* || 5.4.*",
        "illuminate/support": "5.1.* || 5.2.* || 5.3.* || 5.4.*",
        "league/flysystem-aws-s3-v3": "~1.0",
        "nesbot/carbon": "^1.0"
    }
}

The structure of the package is:

-src
   -Storage
      -S3FileStorage.php

In the S3FileStorage.php file I have the following class:

namespace Calcanotica\Storage;

class S3FileStorage { ... }

But, when I try to use the class inside another application as \Calcanotica\Storage\S3FileStorage, I get a Class \Calcanotica\Storage\S3FileStorage not found error.

I already execute composer dump-autoload.

What's the problem?

2
  • 1
    side note: most of your require entries makes no sense. Please read getcomposer.org/doc/articles/versions.md Commented Apr 20, 2017 at 6:50
  • thanks @MarcinOrlowski I would check it. Commented Apr 20, 2017 at 6:51

2 Answers 2

2

Your autoload section is incorrect. Namespace must end with \\ (docs) so it should be:

"autoload": {
    "psr-4": { "Calcanotica\\": "src" }
},

however I'd replace the whole

"autoload": {
    "psr-4": { "Calcanotica": "src" }
},

with

"autoload": {
    "classmap": [ "src/" ]
},

and let composer figure out what is where (docs), which in general use is better, less error prone than setting up namespace mapping by hand and won't require any future attention if you add new namespace to your package.

Sign up to request clarification or add additional context in comments.

2 Comments

That worked! thanks. Could you give a brief explanation on why it wasn't working and now it does? or point to a reference...
perfect @MarcinOrlowski. Than you!
0

In your composer.json file add following lines :

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

}, 

You need to add \\ after your namespace and / after your src. For more check here : https://getcomposer.org/doc/04-schema.md#psr-4

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.