felloz's avatar

Laravel 8 Error: Target class [HomeController] does not exist

Hi, i just installed laravel 8, so I tryed to use Laravel/ui with vue but the home controller is giving me this error:

Illuminate\Contracts\Container\BindingResolutionException
Target class [HomeController] does not exist.
http://127.0.0.1:8000/home

I uset used this commands to install Laravel ui

$ composer require laravel ui

$ php artisan ui vue --auth

$ npm install 

$ npm run dev

After that use the migration but it didn't worked. Any help is apreciated.

0 likes
29 replies
MichalOravec's avatar
Level 75

To App/Providers/RouteServiceProvider.php add $namespace

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';
}

Docs: https://laravel.com/docs/8.x/releases (look for Routing Namespace Updates)

Edited: Also don't forget to add namespace in the boot method of RouteServiceProvider

/**
 * Define your route model bindings, pattern filters, etc.
 *
 * @return void
 */
public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::middleware('web')
            ->namespace($this->namespace) // this line
            ->group(base_path('routes/web.php'));

        Route::prefix('api')
            ->middleware('api')
            ->namespace($this->namespace) // this line
            ->group(base_path('routes/api.php'));
    });
}
22 likes
felloz's avatar

So if i want to upgrade to Laravel 8 from 7 i have to change all my routes???????????? O.o

MichalOravec's avatar

No you don't have to. Just add what I posted before.

1 like
Neeraj1005's avatar

@felloz In laravel8 defining the routing is little bit changed: check documentation https://laravel.com/docs/8.x/routing#basic-routing

#In the previous version of laravel

<?php
Route::resource('home', 'HomeController'); // It is okkk

# In laravel 8 you have to define route like this

<?php

use App\Http\Controllers\HomeController; 


Route::resource('home', HomeController::class);

and you can also use this technique as @michaloravec discussed yet

To App/Providers/RouteServiceProvider.php add $namespace

felloz's avatar

It didnt work i had to add it in the web.php like nakov published

felloz's avatar

Check:

class RouteServiceProvider extends ServiceProvider
{
    /**
     * The path to the "home" route for your application.
     *
     * This is used by Laravel authentication to redirect users after login.
     *
     * @var string
     */
    public const HOME = '/home';

    /**
     * If specified, this namespace is automatically applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

web.php

<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route;
/*
|--------------------------------------------------------------------------
| Web Routes
|--------------------------------------------------------------------------
|
| Here is where you can register web routes for your application. These
| routes are loaded by the RouteServiceProvider within a group which
| contains the "web" middleware group. Now create something great!
|
*/

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', 'HomeController@index');

Keep lanunching same error.

TimHuey's avatar

Don't do this post(SEE BELOW).. see next response. I corrected it and this might be whats tripping everyone up.

Yes the instructions here are wrong. The Protected namespace is coded wrong, it should be....

protected $namespace = 'App\Http\Controllers';

Even better would be to find that line in your RouteServiceProvider.php file and un-comment it. But if you can't find it, add it as coded above just before your boot function.

TimHuey's avatar

Wow nevermind...it was probably correctly coded but I just looked at mine and this message board is changing the code when you post. here it is again...where you see one \ make it 2 slashes. For some reason the message board is replacing double \ with single slash.

protected $namespace = 'App\ \Http\ \Controllers';

remove the space between the slashes.

Neeraj1005's avatar

@felloz

just change this into this

<?php

use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Route; 
use App\Http\Controllers\HomeController; # don't forgot to add this

Route::get('/', function () {
    return view('welcome');
});

Auth::routes();

Route::get('/home', [HomeController::class, 'index']);
1 like
felloz's avatar

Thanks for your response, I really appreciated but saddly I have a system where i have more than 4000 routes, this isnt a solution viable for me, im trying to do what Michal recommend and is in domcumention but isn't working :-(

If you prefer Laravel 7.x style controller route prefixing, you may simply add the $namespace property into your application's RouteServiceProvider.
Neeraj1005's avatar

@felloz I think, Starting new project with laravel8 is good but somewhere upgrading old laravel project into 8 is much confusing because in laravel8 most of the part is changed...

1 like
felloz's avatar

Fixed, i had to add the property in the boot method like this:

public function boot()
    {
        $this->configureRateLimiting();

        $this->routes(function () {
            Route::middleware('web')
                ->namespace($this->namespace)
                ->group(base_path('routes/web.php'));

            Route::prefix('api')
                ->middleware('api')
                ->group(base_path('routes/api.php'));
        });
    }
2 likes
Neeraj1005's avatar

@felloz ohhh, we missed this part. As defined in the previous version...

->namespace($this->namespace)

glad you solved this :)

Edited

In RouteServiceProvider just adding these values

protected $namespace = 'App\Http\Controllers'; # add this


public function boot()
{
    $this->configureRateLimiting();

    $this->routes(function () {
        Route::middleware('web')
            ->namespace($this->namespace) # add this
            ->group(base_path('routes/web.php'));

        Route::prefix('api')
            ->middleware('api')
            ->group(base_path('routes/api.php'));
    });
}

2 likes
felloz's avatar

Please edit the full answer to choose as best answer.

1 like
extjac's avatar

Does anyone know why they change the Controller? Why we need to do this now?

1 like
samo1374's avatar

hi, i found the answer and that was too funny.you just need to change your controller name to every other thing else this.for example:change this: Route::get('/', [HomeController::class,'Home']);

To:

Route::get('/', [LandingController::class,'Home']);

after that you must use its namespace:

use App\Http\Controllers\LandingController;

Md saiful islam's avatar

In your web.php file use it : use App\Http\Controllers\HomeController; then Route::get('/home',[HomeController::class,'index']);

huynhtuson's avatar

May be try let route inside by Group , I think Laravel > 8.0 disable namespace is for the modern web API / headless CMS / React...

routes/web.php

Route::group(['namespace'=>'App\Http\Controllers', 'prefix'=>'/' ], function()
{
			Route::get('/', [HomeController::class,'home']);
			Route::resource('/test', 'Test');   
});
UcheGodspower's avatar

CREATE HomeController USING THIS COMMAND ** php artisan make:controller HomeController --resource

--> HomeController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HomeController extends Controller { /** * Display a listing of the resource. */ public function index() { $login_details = [ 'username' => " ", "password" => " " ]; return view('welcome', ['login_detail' => $login_details]); }

/**
 * Show the form for creating a new resource.
 */
public function create()
{
    //
}

/**
 * Store a newly created resource in storage.
 */
public function store(Request $request)
{
    //
}

/**
 * Display the specified resource.
 */
public function show(string $id)
{
    //
}

/**
 * Show the form for editing the specified resource.
 */
public function edit(string $id)
{
    //
}

/**
 * Update the specified resource in storage.
 */
public function update(Request $request, string $id)
{
    //
}

/**
 * Remove the specified resource from storage.
 */
public function destroy(string $id)
{
    //
}

}

--> web.php

use Illuminate\Support\Facades\Route; use App\Http\Controllers\WelcomeController;

Route::get('/', [WelcomeController::class, 'index']);

Please or to participate in this conversation.