0

I have just installed Laravel 8 and created a fresh new app. I didn't change any file except web.php and added IndexController.php. In web.php the only code I have is: <?php

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

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

In IndexController.php I have:

<?php

namespace Illuminate\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    //
    public function index() 
    {
        return 'This is a controller';
    }
}

If I go to localhost:8000 it throws Target class [IndexController] does not exist. I've tried everything that it is to try: modify RouteServiceProvider.php, clear cache, delete IndexController and recreate it. Nothing. I'm still getting the same message.

  • 1
    You have to import the `IndexController` in your `web.php` file. – Peppermintology Dec 02 '20 at 12:10
  • Import it? how? – Fotea Mihai Dec 02 '20 at 12:21
  • `use Illuminate\Http\Controllers\IndexController;` Not sure why you've use that `namespace` though, convention is `App\Http\Controllers`. – Peppermintology Dec 02 '20 at 12:38
  • I didn't choose that namespace. I want to create a website for chimneys, pellet and wood stoves. Here are the commands that I've used in cmd: `composer create-project laravellaravel test dev-develop` `php artisan storage: link` `php artisan key:generate` `php artisan serve` – Fotea Mihai Dec 02 '20 at 12:46
  • You should use the `artisan` command `php artisan make:controller IndexController` to generate your `controller`. It will be added to the `app\Http\Controllers` folder and you then just import your controller in your `web.php` routes file as described above (`use App\Http\Controllers\IndexController;`). – Peppermintology Dec 02 '20 at 12:55

3 Answers3

0

You should change the namespace of IndexController to namespace App\Http\Controllers;

If you create your controller with php artisan make:controller, it will add proper namespace to your controllers.

SEYED BABAK ASHRAFI
  • 4,093
  • 4
  • 22
  • 32
0

Have you cleared routing while those try outs?

php artisan route:clear

Also you may need others;

php artisan view:clear
php artisan cache:clear
php artisan config:clear
benancetin
  • 11
  • 1
  • no success. Here are the images with the three files: https://imgur.com/wcRpoiQ, https://imgur.com/drMuEWw, https://imgur.com/AGtBtek – Fotea Mihai Dec 02 '20 at 12:35
0

You need to change the namespace on the IndexController file, assuming that the controller is located at app/Http/Controllers/IndexController.php.

Illuminate namespace is used by Laravel framework and files related to it are in vendor/laravel directory

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class IndexController extends Controller
{
    //
    public function index() 
    {
        return 'This is a controller';
    }
}

Next since Laravel 8 the default namespace in RouteServiceProvider is not set to App\Http\Controllers so there are 3 options:

  • import use statement use App\Http\Controllers\IndexController in web.php
  • set the protected $namespace = 'App\Http\Controllers; in the RouteServiceProvider to get the pre v8 behaviour. However not setting the default namespace in RouteServiceProvider is a welcome change
  • set the namespace for Route groups
Route::namespace('App\Http\Controllers')
    ->group(function(){
       //define the routes here for those corresponding to controllers
       //which are within App\Http\Controllers namespace
    });
use Illuminate\Support\Facades\Route;
//use Illuminate\Http\Controllers;
use App\Http\Controllers\IndexController;

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

After making these changes it's better to run composer dump-autoload.

And if route caching is on then it needs to be busted php artisan route:clear

Donkarnash
  • 12,433
  • 5
  • 26
  • 37