Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

rw_lara's avatar

Service Provider Not Found

I downloaded the following project from github

https://github.com/jaiwalker/setup-laravel5-package

Which basically shows you how to set up a basic package in Laravel 5.

However I get the following error:

FatalErrorException in ProviderRepository.php line 150:

Class 'jai\Contact\ContactServiceProvider' not found

I have contacted the creator and just waiting on a solution from them but thought I would see if anyone else could quickly give me a solution.

I think it may have something to do with psr-4 auto loading but I am not sure.

Thanks

0 likes
42 replies
mstnorris's avatar

@timlees Have you added it to your config/app.php file?

http://stackoverflow.com/questions/28662662/creating-a-new-serviceprovider-facade-as-a-package-in-laravel-5

the secret sauce here was first add the service provider to /config/app.php then, second run php artisan vendor:publish


If you've edited the composer.son file you will also have to run composer dump-autoload -o

Also please check out these Guidelines for posting on Laracasts.com

1 like
rw_lara's avatar

Yes, I have the following in my app.php file

    'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        'Illuminate\Bus\BusServiceProvider',
        'Illuminate\Cache\CacheServiceProvider',
        'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
        'Illuminate\Routing\ControllerServiceProvider',
        'Illuminate\Cookie\CookieServiceProvider',
        'Illuminate\Database\DatabaseServiceProvider',
        'Illuminate\Encryption\EncryptionServiceProvider',
        'Illuminate\Filesystem\FilesystemServiceProvider',
        'Illuminate\Foundation\Providers\FoundationServiceProvider',
        'Illuminate\Hashing\HashServiceProvider',
        'Illuminate\Mail\MailServiceProvider',
        'Illuminate\Pagination\PaginationServiceProvider',
        'Illuminate\Pipeline\PipelineServiceProvider',
        'Illuminate\Queue\QueueServiceProvider',
        'Illuminate\Redis\RedisServiceProvider',
        'Illuminate\Auth\Passwords\PasswordResetServiceProvider',
        'Illuminate\Session\SessionServiceProvider',
        'Illuminate\Translation\TranslationServiceProvider',
        'Illuminate\Validation\ValidationServiceProvider',
        'Illuminate\View\ViewServiceProvider',

        /*
         * Application Service Providers...
         */
        'App\Providers\AppServiceProvider',
        'App\Providers\BusServiceProvider',
        'App\Providers\ConfigServiceProvider',
        'App\Providers\EventServiceProvider',
        'App\Providers\RouteServiceProvider',

//       local package service providers
        'jai\Contact\ContactServiceProvider'

    ],

I have also ran composer dump-autoload multiple times.

You can view all the code I am using in the link I mentioned.

Any other ideas what it could be?

rw_lara's avatar

When I run php artisan vendor::publish as you mentioned, I get the following in my command line:

rwdev@ubuntu-rwdev:/var/www/setup-laravel5-package$ php artisan vendor:publish
PHP Fatal error:  Class 'jai\Contact\ContactServiceProvider' not found in /var/www/setup-laravel5-package/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php on line 150
PHP Fatal error:  Uncaught exception 'UnexpectedValueException' with message 'The stream or file "/var/www/setup-laravel5-package/storage/logs/laravel-2015-05-27.log" could not be opened: failed to open stream: Permission denied' in /var/www/setup-laravel5-package/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php:84
Stack trace:
#0 /var/www/setup-laravel5-package/vendor/monolog/monolog/src/Monolog/Handler/RotatingFileHandler.php(88): Monolog\Handler\StreamHandler->write(Array)
#1 /var/www/setup-laravel5-package/vendor/monolog/monolog/src/Monolog/Handler/AbstractProcessingHandler.php(37): Monolog\Handler\RotatingFileHandler->write(Array)
#2 /var/www/setup-laravel5-package/vendor/monolog/monolog/src/Monolog/Logger.php(265): Monolog\Handler\AbstractProcessingHandler->handle(Array)
#3 /var/www/setup-laravel5-package/vendor/monolog/monolog/src/Monolog/Logger.php(543): Monolog\Logger->addRecord(400, 'exception 'Symf...', Array)
#4 /var/www/setup-laravel5-package/vendor/laravel/framework/src/Illuminate/Foundation/Exception in /var/www/setup-laravel5-package/vendor/monolog/monolog/src/Monolog/Handler/StreamHandler.php on line 84


mstnorris's avatar

So it looks like the composer update didn't work.

If you didn't already check out the guidelines I would suggest to do so.

What is it that you're trying to achieve? What is this package used for? If you just want to learn how it is done then I would scrap that and start here - https://laracasts.com/lessons/extracting-packages

rw_lara's avatar

@mstnorris

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "laravel/framework": "5.0.*"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "jai\\Contact\\":"packages/jai/Contact/src/"

        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php -r \"copy('.env.example', '.env');\"",
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

mstnorris's avatar

You need

"Jai\\Contact\\": "packages/jai/contact/src/",
rw_lara's avatar

@mstnorris

All I want to do is create a simple Laravel 5 package.

I have read lots of different online tutorials, watched laracast videos, and even as I mentioned, cloning a repository from git hub of a basic laravel 5 package and every time I have service provider not found.

Thanks for you time.

1 like
bobbybouwmann's avatar

This package is really inconsistent with capital letters, I think that might be a problem! Are you on Windows?

1 like
mstnorris's avatar

@bobbybouwmann yes it is.

@timlees personally, if you're trying to create a package, don't use someone else's. Especially if they can't get the difference between "j" and "J", I certainly wouldn't trust their documentation to tell me everything I need.

Let's go back to the beginning, what is it you want to achieve?

  1. You want to create a package... But I ask, what for? What will it do?
rw_lara's avatar

@mstnorris I have now changed the j in jai to be capital in both app.php and composer.json

app.php

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        'Illuminate\Foundation\Providers\ArtisanServiceProvider',
        'Illuminate\Auth\AuthServiceProvider',
        'Illuminate\Bus\BusServiceProvider',
        'Illuminate\Cache\CacheServiceProvider',
        'Illuminate\Foundation\Providers\ConsoleSupportServiceProvider',
        'Illuminate\Routing\ControllerServiceProvider',
        'Illuminate\Cookie\CookieServiceProvider',
        'Illuminate\Database\DatabaseServiceProvider',
        'Illuminate\Encryption\EncryptionServiceProvider',
        'Illuminate\Filesystem\FilesystemServiceProvider',
        'Illuminate\Foundation\Providers\FoundationServiceProvider',
        'Illuminate\Hashing\HashServiceProvider',
        'Illuminate\Mail\MailServiceProvider',
        'Illuminate\Pagination\PaginationServiceProvider',
        'Illuminate\Pipeline\PipelineServiceProvider',
        'Illuminate\Queue\QueueServiceProvider',
        'Illuminate\Redis\RedisServiceProvider',
        'Illuminate\Auth\Passwords\PasswordResetServiceProvider',
        'Illuminate\Session\SessionServiceProvider',
        'Illuminate\Translation\TranslationServiceProvider',
        'Illuminate\Validation\ValidationServiceProvider',
        'Illuminate\View\ViewServiceProvider',

        /*
         * Application Service Providers...
         */
        'App\Providers\AppServiceProvider',
        'App\Providers\BusServiceProvider',
        'App\Providers\ConfigServiceProvider',
        'App\Providers\EventServiceProvider',
        'App\Providers\RouteServiceProvider',

//       local package service providers
        'Jai\Contact\ContactServiceProvider'

    ],

composer.json

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "laravel/framework": "5.0.*"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\\": "app/",
            "Jai\\Contact\\":"packages/jai/Contact/src/"

        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php -r \"copy('.env.example', '.env');\"",
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

But I still get same error

mstnorris's avatar

Have you run composer dump-autoload -o since changing it?

Can you see the files in the vendor directory? Has it actually been pulled in?

rw_lara's avatar

@ mstnorris

I am working for a web agency and I am just trying to set up my first Laravel 5 package.

For now it could be as simple as creating a simple routes file, controller file, and single view just to say 'Hello World'.

I have worked on Laravel 4 for the past 12 months and never had a problem.

As soon I have tried using Laravel 5 and psr-4 autoloading for packages, I cant seem to get it working.

mstnorris's avatar

Then it isn't a package you want!

From your last response, all you want is to create a simple laravel project. NOT a package.

rw_lara's avatar

@mstnorris

I believe it has been pulled in as this is in my autoload_psr4.php file in my vendor/composer directory:


// autoload_psr4.php @generated by Composer

$vendorDir = dirname(dirname(__FILE__));
$baseDir = dirname($vendorDir);

return array(
    'XdgBaseDir\\' => array($vendorDir . '/dnoegel/php-xdg-base-dir/src'),
    'SuperClosure\\' => array($vendorDir . '/jeremeamia/SuperClosure/src'),
    'Stringy\\' => array($vendorDir . '/danielstjules/stringy/src'),
    'Monolog\\' => array($vendorDir . '/monolog/monolog/src/Monolog'),
    'League\\Flysystem\\' => array($vendorDir . '/league/flysystem/src'),
    'Jai\\Contact\\' => array($baseDir . '/packages/jai/Contact/src'),
    'Illuminate\\' => array($vendorDir . '/laravel/framework/src/Illuminate'),
    'ClassPreloader\\' => array($vendorDir . '/classpreloader/classpreloader/src'),
    'App\\' => array($baseDir . '/app'),
);

rw_lara's avatar

@mstnorris

I appreciate I dont need a package for that.

But I have a load of packages I will need to create soon and before I started them, I just wanted to create the most basic package possible so that I know I can get psr-4 auto loading working.

Thanks

rw_lara's avatar

@ bobbybouwmann

I am using Linux on a virtual machine running on my Windows computer.

bobbybouwmann's avatar

Oke, I will show you how to create a package ;) Give me a few minutes to typ it out

1 like
rw_lara's avatar

@mstnorris

I tried the code anchor one but it didn't seem to work.

I read the laracast tutorial you mentioned as well. And that worked however they put all their files in the /src folder and when I tried to put my controller in a separate controller folder within src I was getting errors again.

So my folder structure when doing the laracast video was

/packages/vender-name/package-name/src/controllers

Where there's was

/packages/vender-name/package-name/src

However I didn't want all my files in the src directory. I wanted them in controllers folder, models folder etc.

mstnorris's avatar

You wouldn't usually do that if you're developing a package. A package, as far as I understand it is to be used by others too, in their projects, so it is kept within the vendor directory so to be able to be kept up to date.

bobbybouwmann's avatar

How to setup a package

Make sure you have a clean install of Laravel, we start clean ;) Now let's create a package! First you need to create two directories. The first one in the vendor directory. This will be the vendor name of the package. Now you add a new new folder with the package name in the created directory from the step before.

Small example: I will create a role and permission package that I will call Entry. Now my vendor is bobbybouwmann and my package name is entry. All lower case!

-- vendor
------ bobbybouwmann
---------- entry    

Now that we have done that we need to create a composer.json in the entry directory and will it with this

{
  "name": "bobbybouwmann/entry",
  "require": {
    "php": ">=5.4.0",
    "illuminate/console": "~5.0",
    "illuminate/support": "~5.0"
  },
  "autoload": {
    "psr-4": {
      "Bobbybouwmann\\Entry\\": "src/Entry"
    }
  },
  "minimum-stability": "dev"
}

Now that you have done that we can create a src directory for source of the package. In this directory we create a EntryServiceProvider.php. The directory structure now looks like this

-- vendor
------ bobbybouwmann
---------- entry    
-------------- src
----------------- Entry
--------------------- EntryServiceProvider.php
-------------- composer.json

Next we need to fill the ServiceProvider with some code

<?php namespace Bobbybouwmann\Entry;

use Illuminate\Support\ServiceProvider;

class EntryServiceProvider extends ServiceProvider {

    /**
     * Bootstrap the application events.
     */
    public function boot()
    {

    }

    /**
     * Register the service provider.
     */
    public function register()
    {
        dd('Hello World');
    }   

}

As you can see I didn't fill the package since I don't know what your intensions are! But it will be setup correctly. Now the most important step is to add the service provider to config/app.php

'providers' => [
        
    // Other providers

    'Bobbybouwmann\Entry\EntryServiceProvider',
    
],

That's it ;) You can now do whatever you want in the package! If you need help setting up a facade and publishing config/views and so on let me know ;)

2 likes
mstnorris's avatar

So I refer back to my previous question, what are you wanting to do exactly, in the larger sense.

Why are you creating a package?

rw_lara's avatar

@mstnorris

We are creating an e-commerce system. So we wanted it to be as modular as possible. For example a package for products, a package for categories, a package for sales and promotions.

Making it modular like this means we can sell different sized ecommerce systems based on what the client would like to pay, and just remove out any packages (functionality) that was not needed for the current clients ecommerce system.

rw_lara's avatar

@bobbybouwmann

Thanks for the code.

I will try that out now.

How would I go about adding a controller to this. So for example image I want to add a controllers folder to the src folder and put a controller file in there. Would I need to change anything in the composer.json file and also what would the namespace of this controller be?

Thanks

bobbybouwmann's avatar

If you want controllers and other stuff you just place them in the src directory! It's just a best practise ;)

-- vendor
------ bobbybouwmann
---------- entry    
-------------- src
----------------- Entry
--------------------- Models
------------------------ Role
------------------------ Permission
--------------------- Controllers
------------------------- UsersController
--------------------- EntryServiceProvider.php
-------------- composer.json

If you do it like this you won't have to change the composer.json file!

Note: you need to use the correct namespaces here!

rw_lara's avatar

@bobbybouwmann

I created a basic package using the code you sent and I still get this error:

FatalErrorException in ProviderRepository.php line 150: Class 'Bobbybouwmann\Entry\EntryServiceProvider' not found

Do I need to add something to the root composer.json as well or just the composer.json in the package?

Thanks

Next

Please or to participate in this conversation.