0

Im starting to programming in Laravel and trying to understood how the routes works. But it always said that the class "UserControler" that I just created it doesn't exist and I don't know why.

Routes > web.php

<?php

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('/user',[UserController::class, 'index']);


I have this in the controllers directory that I just made with the artisan. app>http>controller>UserController.php

<?php

namespace App\Http\Controllers;



class UserController extends Controller
{
    public function index()
    {
        return "Hello World!";
    }
}

I get this error:

BindingResolutionException PHP 8.1.4 9.9.0 Target class [UserController] does not exist.

danieski
  • 31
  • 3
  • Does this answer your question? [Target class controller does not exist - Laravel 8](https://stackoverflow.com/questions/63807930/target-class-controller-does-not-exist-laravel-8) – Marleen Apr 21 '22 at 09:23
  • So basically i just miss to use the URL where is the controller, I was using the default route, thank you . – danieski Apr 21 '22 at 09:30
  • @danieski have you ever checked your laravel version please share with us – Rachna Gajjar Sep 22 '22 at 04:53

3 Answers3

2

You forgot to import the controller to your route files. Currently your web.php file can't resolve UserController class, because it doesn't know what it is. You can import it using the use keyword:

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\UserController; //This line
zlatan
  • 3,346
  • 2
  • 17
  • 35
2

just add your class namespace lik:

<?php

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

/*
|--------------------------------------------------------------------------
| 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('/user',[UserController::class, 'index']);
Keyvan Gholami
  • 168
  • 1
  • 3
  • 15
0

what zlatan posted was exactly what i forgot too.... It's so obvious to declare it that i didn't see it anymore...

so make sure you declare use App\Http\Controllers\UserController; in the web.php file.

oehTie
  • 1
  • Please don't add "thank you" as an answer. Once you have sufficient [reputation](https://stackoverflow.com/help/whats-reputation), you will be able to [vote up questions and answers](https://stackoverflow.com/help/privileges/vote-up) that you found helpful. - [From Review](/review/late-answers/32775697) – Garric Sep 27 '22 at 18:30