Prakash Raman
Prakash Raman

Reputation: 13923

Where is the right place to put files in Laravel?

I am new to Laravel and am trying to find my way around.

I want to create multiple custom Exception classes. I am a little confused as to where they should reside.

For now, I have created a folder called 'exceptions' in app and added the path to ClassLoader::addDirectories. I could really do with some advice.

Upvotes: 3

Views: 211

Answers (2)

judereid
judereid

Reputation: 901

Laravel doesn't have a set place to store custom exceptions, you can place them anywhere you like.

Creating an app/exceptions/ directory works fine - You can autoload them all in your global.php file by adding

app_path() . '/exceptions/'

to the ClassLoader::addDirectories array.

If you have a lot and prefer to be more organised you can namespace your exception classes and take advantage of PSR-4 autoloading with composer.

Upvotes: 1

Alex O.
Alex O.

Reputation: 1192

Are you using Composer? For example, using Composer you can put your configuration for autoloading:

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/database/migrations",
        "app/database/seeds"
    ],
    "psr-4": {
        "Exceptions\\": "src/Exceptions",
        "Services\\": "src/Services",
        "Api\\": "src/Api"
    }
},

As result your Exception file /src/Exceptions/Specific/ExtraSpecificException.php will be available

namespace Exceptions\Specific;


class ExtraSpecificException extends \Exception
{}

Upvotes: 2

Related Questions