Christian Giupponi
Christian Giupponi

Reputation: 7618

Add custom package to composer.json file

I have a custom package that is not uploaded on github or packagist and I need to add it to a Laravel 5.1 project.

This is my package folder structure:

Packages
   \_ christian
     \_ smsservice
       \_src
         \_ Facades
           \_ MySMS.php
         \_ SMSServiceServiceProvider.php
       \_ vendor
         \_ composer
         \_ autoload.php

I have edited my root composer.json to add the package:

"psr-4": {
    "App\\": "app/",
    "Christian\\SMSService\\": "app/Packages/christian/smsservice/src/"
},

Then I have added the service provider and the facade to the app.php file but when I try to use the package I get:

FatalErrorException in ProviderRepository.php line 146:
Class 'Christian\SMSService\SMSServiceServiceProvider' not found

But the ServiceProvider exists and the namespace is correct:

namespace Christian\SMSService;


use Illuminate\Support\ServiceProvider;
use Illuminate\Routing\Router;

class SMSServiceServiceProvider extends ServiceProvider {
  //Code
}

Upvotes: 4

Views: 2136

Answers (1)

Cyril de Wit
Cyril de Wit

Reputation: 446

I needed that functionality too. I'm using the following code for one of my local Laravel projects:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "repositories": [
        {
            "type": "path",
            "url": "../../GitHub/laravel-page-visits-counter"
        }
    ],
    "require": {
        "php": ">=5.6.4",
        "laravel/framework": "5.4.*",
        "laravel/tinker": "~1.0",
        "cyrildewit/laravel-page-visits-counter": "dev-master"
    },
    "require-dev": {
        "fzaninotto/faker": "~1.4",
        "mockery/mockery": "0.9.*",
        "phpunit/phpunit": "~5.7"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/"
        }
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },
    "scripts": {
        "post-root-package-install": [
            "php -r \"file_exists('.env') || copy('.env.example', '.env');\""
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ],
        "post-install-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postInstall",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "Illuminate\\Foundation\\ComposerScripts::postUpdate",
            "php artisan optimize"
        ]
    },
    "config": {
        "preferred-install": "dist",
        "sort-packages": true,
        "optimize-autoloader": true
    }
}

Upvotes: 2

Related Questions