Home > Net >  Laravel Controller does not exists
Laravel Controller does not exists

Time:04-21

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.

CodePudding user response:

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']);

CodePudding user response:

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
  • Related