Class App\Http\Controllers\StudentController does not exist in Laravel 5

35,621

Solution 1

Although laravel is magic at times, it only works if you stick the the default configuration and conventions.

You can place your controllers anywhere (heck, even load from a database and eval them) but you have to change the configuration accordingly.

I suspect you have the wrong namespace configured in RouteServiceProvider. By default it is App\Http\Controllers.

Changing default folder

If all your controllers will be in the same folder, change it to App\Student\Controllers and forget about it.

class RouteServiceProvider extends ServiceProvider
{
    // ...
    protected $namespace = 'App\Student\Controllers';
    // ...
}

Multiple modules

If you want to have multiple modules, then change your RotueServiceProvider namespace config to App and in route files use Student\Controllers\StudentController@list

class RouteServiceProvider extends ServiceProvider
{
    // ...
    protected $namespace = 'App';
    // ...
}

Route::get('/list', 'Student\Controllers\StudentController@list');

Solution 2

go to RouteServiceProvider.php and the changed the namespace to default

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

Solution 3

it gives you that error because the controller couldn't find the class you are calling .. in the top of your controller add

use App\Student;

to make it work ..

Solution 4

You create controller in wrong location. Default Controller location is :

app/Http/Controllers
Share:
35,621
I am the Most Stupid Person
Author by

I am the Most Stupid Person

I am the person who build this world (earth) just before million of years. So I own the whole things including SO in this world.

Updated on July 09, 2022

Comments

  • I am the Most Stupid Person
    I am the Most Stupid Person almost 2 years

    I am building a module called Student in Laravel.

    I use the routes.php file inside the Student folder to write routes realted to student module..

    When I use just Route::get('/list', function () { return view('welcome');}); program working fine without error.

    But when I am using Route::get('/list', 'StudentController@list'); there is a error.

    Error is,

    Class App\Http\Controllers\StudentController does not exist

    Folder Structure

    enter image description here

    Student Controller

    namespace App\Student\Controllers;
    
    use Illuminate\Http\Request;
    use App\Http\Controllers\Controller;
    
    class StudentController extends Controller
    {
        public function list(){
            echo "Hello"
        }
    }
    

    Student Service Provider

    namespace App\Student;
    
    use App\Providers\RouteServiceProvider as ServiceProvider;
    use Illuminate\Support\Facades\Route;
    
    class StudentServiceProvider extends ServiceProvider
    {
        /**
         * Bootstrap the application services.
         *
         * @return void
         */
        public function boot()
        {
            parent::boot();
        }
    
        /**
         * Register the application services.
         *
         * @return void
         */
        public function register()
        {
            //
        }
    
        /**
         * Define the routes for the application.
         *
         * @internal param Router $router
         */
    
        public function map()
        {
            Route::group([
                'namespace' => $this->namespace,
                'prefix' => 'students',
            ], function ($router) {
                require __DIR__ . '/routes.php';
            });
        }
    }