1
votes

I'm building a laravel package, but it won't autoload.

This is the autoload section of my package:

"autoload": {
    "classmap": [
        "src"
    ],
    "psr-4": {
        "Doitonlinemedia\\Translatableroutes\\": "src/"
    }
}

Classes are namespaced like Doitonlinemedia\Translatableroutes

Folder structure is like:

- vendor
- - doitonlinemedia
- - - translatableroutes
- - - - composer.json
- - - - src
- - - - - class1.php

I've added the ServiceProvider in app.php like: Doitonlinemedia\Translatableroutes\TranslatableRoutesServiceProvider::class

But it keeps saying Class 'Doitonlinemedia\Translatableroutes\TranslatableRoutesServiceProvider' not found

Run composer dump-autoload on every change.

What am I doing wrong?

EDIT

For development I added: "Doitonlinemedia\\Translatableroutes\\": "packages/doitonlinemedia/translatableroutes/src" to the main composer.json file. But when I require this composer package into a new project I'm getting the above problems.

To try out this package: https://github.com/doitonlinemedia/TranslatableRoutes and follow the instructions

2

2 Answers

4
votes

PHP namespaces are case sensitive. You have to put this into your composer.json

"psr-4": {
    "Doitonlinemedia\\Translatableroutes\\": "src/"
}

instead of this

"psr-4": {
    "doitonlinemedia\\translatableroutes\\": "src/"
}

For me it works also without the classmap option

run composer dump-autoload again and retry.

Edit

This works for me

{
    "name": "doitonlinemedia/translatableroutes",
    "description": "Create translatable resource routes",
    "authors": [
        {
            "name": "Tim van Uum",
            "email": "[email protected]"
        }
    ],
    "minimum-stability": "stable",
    "license": "MIT",
    "require": {
        "php": ">=5.3.0"
    },
    "autoload": {
        "classmap": [
            "src"
        ],
        "psr-4": {
            "Doitonlinemedia\\Translatableroutes\\": "src/"
        }
    }
}

In addition you should add this package to your requirements "illuminate/support": "~5.2".

Note: It seems to composer do not refresh the autoload entries of composer.json files located in vendor packages if they are not updated.

After editing you have to push this into your repository. Reinstall the package in your project.

If you are still developing actively on your package I recommend to setup an environment by following this instructions: Developing your packages in Laravel 5.

It's so much easier to develop your package without reinstalling or updating the remote repository always.

0
votes

Thanks to some help on laracast forum. I found out that my project was bit corrupt I guess, cause the package was already working.

https://laracasts.com/discuss/channels/laravel/trouble-autoloading-custom-package-laravel-52