dulan
dulan

Reputation: 1604

Laravel cannot find self defined service providers

I'm using Laravel for my project, I created a service provider and it's located in app/services/ToolboxServiceProvider.php, and I added

'providers' => array(
    ....
    'services\ToolboxServiceProvider',
);

in my app.php config file. Now when loading the app, it says the service provider cannot find, I know that something's wrong with my path setting in that providers array, question is: how to make it right? Thanks in advance.

{
"name": "laravel/laravel",
"description": "The Laravel Framework.",
"keywords": ["framework", "laravel"],
"license": "MIT",
"require": {
    "laravel/framework": "4.2.*"
},
"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/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 artisan key:generate"
    ]
},
"config": {
    "preferred-install": "dist"
},
"minimum-stability": "stable"

}

Upvotes: 1

Views: 2757

Answers (3)

Marcin Nabiałek
Marcin Nabiałek

Reputation: 111839

You should add into autoload => classmap section of your composer.json:

"app/services",

so it should look like:

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

In your provider file, at the beginning you should have:

<?php namespace services;

(in lower case).

And after those changes you should run:

composer dump-autoload

to rebuild classmap

Upvotes: 7

The Alpha
The Alpha

Reputation: 146191

Create the service provider using something like this in app/services folder, notice the namespace:

<?php

namespace Services;

use Illuminate\Support\ServiceProvider;

class ToolboxServiceProvider extends ServiceProvider
{
    //...
}

Then in the providers array add 'Services\ToolboxServiceProvider'. Then add "app/services" in the classmap section in composer.json file and dump the autoloader.

Upvotes: 1

MattM
MattM

Reputation: 49

What namespace is ToolboxServiceProvider using? Have you added autoloading in the composer.json file? Have you tried a composer dump-autoload?

Upvotes: -1

Related Questions