1

I just made changes to the application structure for my Laravel application. It works well when runnning tests (for the Http controllers). The problem is when i try to run artisan commands (that literally need to access "getNamespace()" method), it wont resolve the namespaces.

Here are the composer.json:

 "autoload": {
        "psr-4": {
            "App\\": "app/",
            "Database\\Factories\\": "database/factories/",
            "Database\\Seeders\\": "database/seeders/",
            "Modules\\": "modules/"
        },
        "files": [
            "app/Helpers/app.php",
            "app/Helpers/form.php",
            "app/Helpers/view.php"
        ]
    },
    "autoload-dev": {
        "psr-4": {
            "Tests\\": "tests/"
        }
    },

I do aware that i can add Modules\ModuleA, Modules\ModuleB to the composer json, but that put alot of work. So i decided to override the getNamespace() method instead, but what is the best way to override single method illuminate/foundation/xxx classes through service provider?

Folder tree:

laravel-project/
├── app/
│   ├── Exception
│   ├── Providers
│   └── ...
├── modules/
│   ├── ModuleA/
│   │   ├── Services
│   │   ├── Http/
│   │   │   ├── Controllers
│   │   │   └── Requests
│   │   └── Models
│   └── ModuleB/
│       └── ...
├── tests
└── ...
  • Can you show the exact tree of Modules folder? – StewieSWS Feb 17 '23 at 13:24
  • Try `composer dump-autoload` – StewieSWS Feb 17 '23 at 13:28
  • That's not really the problem though. As i said before, the application works normally in the browser. The problem comes when you try to invoke the getNamespace() method inside the Application container. So i expect how to override the core container methods – Azvya Erstevan Feb 17 '23 at 13:32
  • You can extend the class and also bind your version to the container... In register method of a service provider... $this->app->bind('Vendor\Class', 'Your\Class'); and in your class override the method – Mahdi Rashidi Feb 17 '23 at 15:38

0 Answers0