Laravel 5 Unable to detect application namespace

I'm new to Laravel 5 and trying to understand it bit by bit and at the moment I'm really confused with error messages. MVC is new thing to me.

What I'm trying to do is blog system for my site and I've downloaded package called "Serverfireteam/blog"; https://phppackages.org/p/serverfireteam/blog

It installed just fine, I guess. When I go to http://myhost.com/public/panel/login I get the login screen but when I login it gives me this error:

ErrorException in Application.php line 1119: Unable to detect application namespace. (View: /var/www/html/mpa2/resources/views/vendor/panelViews/dashboard.blade.php)

&&

RuntimeException in Application.php line 1119: Unable to detect application namespace.

Funny thing is it worked before just fine, I could login and make/edit blog posts and I could see them go to mysql-database. Also I was able to see them on site. Then I started to modify view/template files for integrating blog output to my site's own layout.

I've got no idea what gives, I've tried to google for solution but no help. Any ideas what could be wrong?


Solution 1:

Okay, I solved it. What I did to solve this:

composer update

gave me following error:

[Seld\JsonLint\ParsingException]

"./composer.json" does not contain valid JSON
 Parse error on line 9:
"require-dev
 ---------------------^
 Expected: 'STRING' - It appears you have an extra trailing comma

I opened composer.json and there was one extra comma in last line:

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*",
}

Removed the comma so it looked like this:

"require": {
    "php": ">=5.5.9",
    "laravel/framework": "5.1.*"
}

And problem was gone.

Solution 2:

Usually, this means that your composer.json file contains invalid JSON. Usually an extra comma at the end of an array.

Try running this to tell you exactly where the issue is:

composer diagnose

Solution 3:

laravel version: 5.8.3

[One more Reason]: default app path in composer.json is modified

the default setup looks like this

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

If its modified to say,

"psr-4": {
    "Core\\": "app/Core/"
 },

the make commands with artisan wont work, and a few other things

the reason is https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/Application.php#L296

app is static in the path, and here is the where the exception is thrown https://github.com/laravel/framework/blob/5.3/src/Illuminate/Foundation/Application.php#L1143

This default behavior can be modified in bootstrap/app.php

Here is my solution [reference: https://laracasts.com/discuss/channels/general-discussion/how-i-can-change-laravel-directory-structure?page=1]

Solution:

Core/Application.php

<?php

namespace Core;

use Illuminate\Foundation\Application as IlluminateApplication;

class Application extends IlluminateApplication
{
    protected $appPath = __DIR__;
}

bootstap/app.php

$app = new \Core\Application(
    $_ENV['APP_BASE_PATH'] ?? dirname(__DIR__)
);

Solution 4:

Please Write this Command on the project root composer diagnose This Command Will detect the problem my case I found this

 [Seld\JsonLint\ParsingException]
  "./composer.json" does not contain valid JSON
  Parse error on line 1:
  3:06 PM 08-Dec-20{
  ^
  Expected one of: 'EOF', '}', ',', ']'

Then I removed

3:06 PM 08-Dec-20

Then I have Created Controller Succesfully. I Hope The composer diagnose Command Will Detect Your Problem.

Solution 5:

What caused this for me was having lines commented with //. The // can be on its own line or at the end of the line. Also having comma at the end can cause this.

Removing the comments solved this. And/or removing the extra ending comma.

The error happens for "composer update", and artisan commands such as "php artisan make:controller TestsController --resource", or "php artisan make:model Test"