Jimmyt1988
Jimmyt1988

Reputation: 21186

Laravel and using namespaces - Class not found

I have created a php file under the Models folder provided by Laravel.

Inside I have:

// Filename: Models/Shoopy.php
namespace Whatever\Something;

class Shoopy
{
    public function Toot(){};
}

Now in another Model file I am using that namespace:

use \Whatever\Something\Shoopy;

class AnotherModel
{
    public function Spop()
    {
        $harr = new Shoopy();
    }
}

It comes up with a Laravel error:

class Whatever\Something\Shoopy not found

Any ideas?

Upvotes: 1

Views: 3084

Answers (2)

Simon
Simon

Reputation: 131

I know this is an older issue, but if you go into your composer.json file, you'll probably see something like...

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/"
    }
},

What you can do is tell composer where Whatever should be, say if it was a different location (or sub-folder) from where Laravel normally keeps it's files.

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\\": "app/",
        "Whatever\\": "app/Whatever",
    }
},

Upvotes: 2

Jeff Lambert
Jeff Lambert

Reputation: 24671

Classes need to exist in files with directory paths that match their namespace. This is according to the PSR-0 Standard. In your example, try placing your Shoopy class in the file:

app/models/Whatever/Something/Shoopy.php

And then see if the autoloader can find it.

Upvotes: 1

Related Questions