Laravel: Controller does not exist

neeraj picture neeraj · Feb 19, 2015 · Viewed 60.4k times · Source

I added new controller in /app/controllers/admin/ folder and added the route in /app/routes.php file as well. Then i run the following command to autoload them

php artisan dump-autoload

I got the following error

Mcrypt PHP extension required.

I followed instruction given at https://askubuntu.com/questions/460837/mcrypt-extension-is-missing-in-14-04-server-for-mysql and able to resolve the mcrypt issue.

After that i run the php artisan dump-autoload command but still getting following error

{"error":{"type":"ReflectionException","message":"Class CoursesController does not exist","file":"\/var\/www\/html\/vendor\/laravel\/framework\/src\/Illuminate\/Container\/Container.php","line":504}}

Here is code of my routes.php file

Route::group(array('before' => 'adminauth', 'except' => array('/admin/login', '/admin/logout')), function() {
    Route::resource('/admin/courses', 'CoursesController');
    Route::resource('/admin/teachers', 'TeachersController');    
    Route::resource('/admin/subjects', 'SubjectsController');
});

Here is code of CoursesController.php file

<?php

class CoursesController extends BaseController
{

    public function index()
    {
        $courses = Course::where('is_deleted', 0)->get();
        return View::make('admin.courses.index', compact('courses'));
    }

    public function create()
    {
        return View::make('admin.courses.create');
    }

   public function store()
    {
        $validator = Validator::make($data = Input::all(), Course::$rules);

        if ($validator->fails()) {
            $messages = $validator->messages();
            $response = '';
            foreach ($messages->all(':message') as $message) {
                $response = $message;
            }
            return Response::json(array('message'=>$response, 'status'=>'failure'));
        } else {
            Course::create($data);
            return Response::json(array('message'=>'Course created successfully','status'=>'success'));
        }
    }

    public function edit($id)
    {
        $course = Course::find($id);
        return View::make('admin.courses.edit', compact('course'));
    }

    public function update($id)
    {
        $course = Course::findOrFail($id);
        $validator = Validator::make($data = Input::all(), Course::editRules($id));

        if ($validator->fails()) {
            $messages = $validator->messages();
            $response = '';
            foreach ($messages->all(':message') as $message) {
                $response = $message;
            }
            return Response::json(array('message'=>$response, 'status'=>'failure'));
        } else {
            $course->update($data);
            return Response::json(array('message'=>'Course updated successfully','status'=>'success'));
        }
    }

    public function destroy($id)
    {
        Course::findOrFail($id)->update(array('is_deleted' => '1'));
        return Response::json(array('message'=>'Course deleted successfully','status'=>'success'));
    }

}

Answer

Sushant Aryal picture Sushant Aryal · Feb 19, 2015

Did you add autoload classmap to composer.json file? Open your composer.json file and add

"autoload": {
        "classmap": [
            "app/controllers/admin",
        ]
    }

if you add folders inside controllers, you need to add it to composer.json file. Then run

composer dumpautoload

OR ALTERNATIVE

go to app/start/global.php and add

ClassLoader::addDirectories(array(
    app_path().'/controllers/admin',
));