简体   繁体   中英

Class of laravel package not found

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?

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.

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM