Make runtime error: request for laravel 5

When I run the sample code in laravel docs php artisan make:request StoreBlogPostRequest to create a new check controller, I get the following error

 [RuntimeException] Unable to detect application namespace. 

I'm not sure what happened, I did a few searches, but nothing explains this error. Any ideas?

+5
source share
1 answer

In Laravel 5, an β€œapplication” is a collection of PHP files under one namespace stored in the app/ folder

By default, and in most Laravel 5 code examples from documents, this is the App\ namespace. For example, one controller in your application might look like this.

 namespace App\Http\Controller; class MyController { //... } 

When Laravel generates code (i.e. when you use the make:request command), he needs to know that this is the application namespace (it is possible to change the namespace using the artisan app:name command). For some reason, Laravel 5 cannot determine the namespace on your system.

If you look at the section of the main Laravel 5 code that defines the namespace

 #File: vendor/laravel/framework/src/Illuminate/Console/AppNamespaceDetectorTrait.php protected function getAppNamespace() { $composer = json_decode(file_get_contents(base_path().'/composer.json'), true); foreach ((array) data_get($composer, 'autoload.psr-4') as $namespace => $path) { foreach ((array) $path as $pathChoice) { if (realpath(app_path()) == realpath(base_path().'/'.$pathChoice)) return $namespace; } } throw new RuntimeException("Unable to detect application namespace."); } 

You will see Laravel discover the namespace by looking at your composer.json file and looking for the first true psr-4 namespace.

My guess is that your composer.json file is missing a namespace

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

Add this back and you will be fine.

+9
source

All Articles