简体   繁体   中英

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

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM