Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel: Controller does not exist

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

}
like image 989
neeraj Avatar asked Feb 19 '15 06:02

neeraj


People also ask

Why controller is not found in Laravel?

In short, Laravel controller does not exist error shows up due to improper set up of controllers.

What is controller in laravel?

Controllers can group related request handling logic into a single class. For example, a UserController class might handle all incoming requests related to users, including showing, creating, updating, and deleting users. By default, controllers are stored in the app/Http/Controllers directory.


1 Answers

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',
));
like image 79
Sushant Aryal Avatar answered Sep 30 '22 18:09

Sushant Aryal