Search code examples
phplaravellaravel-4composer-phpautoload

Composer, Laravel and local packages


My issue is I have a package which isn't a repository and I am trying to get it to play nice with Laravel and composer. It is still located under the vendor folder, the only issue is that if I simply set:

"psr-0": {
        "Test\\Test": "vendor/test/test/src/"
    }

This will load the service provider but none of the controllers etc will autoload. What is the correct way to implement a package with larval that does not have it's own repository. Or does this go against the nature of packages and this should simply be structured under the applications controllers.

The package was created by me using workbench but I found i did not really need this as a separate repository but it would still be good to keep it as a package. Therefore the structure is exactly the same as a regular package:

vendor
    testvendor
        testpackage
            public
            src
            tests
            .gitignore
            composer.json
            phpunit.xml

UPDATE:

As a solution for the time being I am using:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php",
        "vendor/package"
    ]
},

As an entry in the class map. Looking forward I will probably refactor this into the app folder or create a repository for this package.


Solution

  • If you have some classes that you're calling "package", you're not supposed to add those files to your vendor folder. This folder is managed by composer and at any time you might loose it. Create a subfolder in your application and put those files there.

    You have to be sure your PSR-0 autoloading will work for every single file in your folder structure. So, if your root is vendor/test/test/src/ and your namespace is

    Test\\Test
    

    All your files must be in

    vendor/test/test/src/Test/Test/ClassFileName.php
    

    PSR-4 is easier to deal and understand, this

    "psr-4": {
        "Test\\Test\\": "vendor/test/test/src/"
    }
    

    Means that your files would have to be like:

    vendor/test/test/src/ClassFileName.php
    

    Doublecheck your namespaces. It's easy to make mistakes when using namespaces with PSR-0 and remember that

    composer dump-autoload
    

    Must be ran every time you change things in composer.json or create new files. If it's a simple class autoloading, every time you create a file, if it's a PSR-X autoloading, everytime you create or update a namespace in your composer.json file.

    If what you have is is really a package you should use Composer: when your package is structured as a composer package (check Laravel's composer.json as an example), the correct way of adding it to your application, if it's not list in Packagist, is via repositories.

    You can have (non-packagist) packages in a public VCS repository:

    {
        "require": {
            "monolog/monolog": "dev-bugfix"
        },
    
        "repositories": [
            {
                "type": "vcs",
                "url": "https://github.com/igorw/monolog"
            }
        ]
    }
    

    You can have (non-packagist) packages in a protected by password VCS repository (git, bitbucket...):

    {
        "require": {
            "vendor/my-private-repo": "dev-master"
        },
        "repositories": [
            {
                "type": "vcs",
                "url":  "[email protected]:vendor/my-private-repo.git"
            }
        ]
    }
    

    You can have your packages zipped in your hard drive and load them via the artifact repository type:

    "repositories": [
        {
            "type": "artifact",
            "url": "path/to/directory/with/zips/"
        }
    ],