Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error running make:request on laravel 5

Tags:

laravel

When running the example code on the laravel docs php artisan make:request StoreBlogPostRequest to create a new validation controller, I get the following error

[RuntimeException]                       
Unable to detect application namespace.  

I'm not sure what's wrong, I've done some searching, but nothing really explains this error. Any ideas?

like image 202
user2075215 Avatar asked Apr 29 '15 22:04

user2075215


People also ask

What is request() in laravel?

Laravel's Illuminate\Http\Request class provides an object-oriented way to interact with the current HTTP request being handled by your application as well as retrieve the input, cookies, and files that were submitted with the request.

How do I create a request in laravel?

First you have to set the request's method to POST: $myRequest = new \Illuminate\Http\Request(); $myRequest->setMethod('POST'); $myRequest->request->add(['foo' => 'bar']); dd($request->foo); By the way using $myRequest->query->add() you can add data to a GET request.

How do I enable errors in laravel?

This can be achieved with the variable APP_DEBUG set in the environment file . env stored at the root of the application. For local environment the value of APP_DEBUG should be true but for production it needs to be set to false to hide errors.


1 Answers

In Laravel 5, an "application" is a collection of PHP files under a single namespace, stored in the folder app/

By default, and in most of the Laravel 5 sample code from the docs, this namespace is App\. 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), it needs to know what this application namespace is (it's possible to change the namespace with the artisan app:name command). For some reason, in your system, Laravel 5 can't detect the namespace.

If you look at the section of Laravel 5 core code that detects 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'll see that Laravel detects the namespace by looking at your composer.json file, and looking for thefirst valid psr-4 namespace.

My guess is your composer.json file is missing the namespace

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

Add that back in, and you'll be good to go.

like image 57
Alan Storm Avatar answered Oct 18 '22 04:10

Alan Storm