Blackbam
Blackbam

Reputation: 19386

Laravel/Lumen PSR-4: If I put classes into subdirectories do I have to use different namespaces then?

Look at the following part of my application structure:

App
-Events
-Http
--Controllers
---Rest
-Services
--Base
--Primary

What I would like to do is to use the namespace App\Services for all services and the namespace App\Http\Controller for all controllers.

This is how my application is loaded with Composer:

"autoload": {
    "psr-4": {
        "App\\": "app/"
    } }

This is the Exception I get:

(1/1) FatalThrowableError Class 'App\Services\CapabilityService' not found

CapabilityService exists for sure, but it is within the subfolder Primary. Is there a way to autoload files from subdirectories into one namespace together? If yes how and is there any important reason not to do it?

This is the CapabilityService:

namespace App\Services;

This is the location:

App/Services/Primary/CapabilityService

Upvotes: 0

Views: 446

Answers (1)

Blackbam
Blackbam

Reputation: 19386

Nvm I figured it out - just do arrays in within composer.json, where you specify all subfolders:

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "App\\Services\\": ["app/services","app/services/base","app/services/primary"],
            "App\\Models\\": ["app/models","app/models/traits"],
            "App\\Http\\Controllers\\": ["app/http/controllers","app/http/controllers/base","app/http/controllers/rest","app/http/controllers/rest/base"]
        },

Better solutions still welcome.

Upvotes: 1

Related Questions