Laravel: Controller does not exist

房东的猫 提交于 2019-12-04 13:39:32

问题


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

}

回答1:


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



回答2:


We need to create controller via command line.

php artisan make:controller nameController --plain.

Before Laravel 5, make namespace is not available. Instead, this works

php artisan controller:make nameController

Execute your command inside your project directory and then create your function.




回答3:


In my case, in the top of my controller code i add this line :

namespace App\Http\Controllers\CustomFolder\ControllerClassName;

and my problem is solved




回答4:


I think your problem has already been fixed. But this is what did.

Structure

Http
  .Auth
  .CustomControllerFolder
    -> CustomController.php

to get this working in your route file make sure you use the correct name space for eg:

  Route::group(['namespace'=>'CustomControllerFolder','prefix'=>'prefix'],
      function() {

   //define your route here
}

Also dont forget to use namespace App\Http\Controllers\CustomControllerFolder in your controller.

That should fix the issue.

Thanks




回答5:


Don't forget to do:

php artisan route:clear

In my case this was the solution when I got this error after making a route code change.




回答6:


I just had this issue because I renamed a file from EmployeeRequestContoller to EmployeeRequestsContoller, but when I renamed it I missed the .php extension!

When I reran php artisan make:controller EmployeeRequestsContoller just to be sure I wasn't going crazy and the file showed up I could clearly see the mistake:

/EmployeeRequestsContoller
/EmployeeRequestsContoller.php

Make sure you have the extension if you've renamed!




回答7:


Sometimes we are missing namespace App\Http\Controllers; on top of our controller code.



来源:https://stackoverflow.com/questions/28599870/laravel-controller-does-not-exist

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!