4

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
}
Christian Giupponi
  • 7,408
  • 11
  • 68
  • 113
  • 1
    Have you refreshed composer autoload after adding your `psr-4`? Try If not, try `composer dump-autoload` and refresh. – Tomas Votruba Nov 03 '15 at 10:11
  • Yes, I tried but with no results – Christian Giupponi Nov 03 '15 at 13:45
  • This path - "Christian\\SMSService\\": **"app/Packages/christian/smsservice/src"** - should be relative to your `composer.json`. Its usually in the same dir as `/vendor`, but your `/vendor` is located at `app/Packages/christian/smsservice`. Could you show us your `composer.json` and where exactly is located? – Tomas Votruba Nov 03 '15 at 17:37

1 Answers1

2

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
    }
}
Cyril de Wit
  • 446
  • 4
  • 12