michi
michi

Reputation: 6625

trouble with ServiceProvider in Laravel4

Being new to Laravel, I followed Repositories Simplified on Laracasts and created a BackendServiceProvider class.

Now, when I want to do

php artisan generate:controller TestController

I get an error:

PHP Fatal error:  Class '_testic\repos\BackendServiceProvider' not found in
/vagrant/vendor/laravel/framework/src/Illuminate/Foundation/ProviderRepository.php
on line 158

What did go wrong? How can I solve it?

What I did so far:

  1. put my service-provider into config/app.php
  2. then added it to composer.json inside psr-0:

    {
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "require": {
        "laravel/framework": "4.1.*"
    },
    "autoload": {
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "psr-0": {
            "_testic": "app/"  // <----- my entry
        },
    "scripts": { ...
    
  3. ran composer dump-autoload -o

Upvotes: 0

Views: 653

Answers (2)

Jonathan Gimeno
Jonathan Gimeno

Reputation: 434

The psr-0 goes inside autoload. Just next to classmap.

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
        "app/tests/TestCase.php"
    ],
    "psr-0": {
        "_testic": "app/"  // <----- my entry
    }
}

Upvotes: 2

Antonio Carlos Ribeiro
Antonio Carlos Ribeiro

Reputation: 87789

Usually those kind of problems are from a malformation in namespaces, file name or directory structure.

You defined a PSR-0 namespacing as:

"_testic": "app/" --> which means 'my _testic namespace is stored in the folder app/'

And, according to the error message, you namespaced your file as

_testic\repos\BackendServiceProvider

So I have to assume that you will use that particular file in your other files as:

use _testic\repos\BackendServiceProvider;

Which also means that you MUST have your BackendServiceProvider.php file in the folder

/whateverRootFoldersYouMayHave/app/_testic/repos/BackendServiceProvider.php

If this is not your folder structure, you will receive this error.

Note that in PSR-0 the whole folder structure must be equal the one you defined as your root namespace.

Upvotes: 1

Related Questions