Laravel Error Message ReflectionException Controller::method() does not exist

Posted on

Facing an error stated as in the title of this article, it is simply an error which can be solved easily. But to be more specific on discussing how to solve the error, first of all, the real situation which become the trigger for the error to be generated will be explained in detail. The error is in the form of an error message which is printed ‘ReflectionException ControllerName::MethodName() does not exist’. Below is the image depicting the error message stated before :

Since accessing a certain URL of the web application for an example where it is shown in the above, ‘/role/create’, it is actually mapped to a certain controller file defined in a route file configuration named web.php located in routes folder for Laravel 5.3, that definition itself must be checked. The location of the file can be described into the following tree format as follows :

user@hostname:/var/www/html/laravel-project$ tree routes
routes
├── api.php
├── console.php
└── web.php

0 directories, 3 files
user@hostname:/var/www/html/laravel-project$

Below is the content of the route file configuration :

//Role
Route::get('/role','RoleController@index'); 
Route::get('/role/create','RoleController@create');
...

So, after checking the content of the route file configuration, there is a URL, ‘/role/create’ where that URL itself is mapped to a method defined inside a controller. The controller is ‘RoleController’ and the method which is used is the ‘create’ method.

To be able to execute the web application by accessing the URL address, it is important to make sure the target of the destination which is defined in the route file configuration which is responsible for further processing. And in the above context, it is delegated to a method name create in a controller named ‘RoleController’. Obviously, the controller itself is actually a file named RoleController.php and it is representing the controller named ‘RoleController’.

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests;

class RoleController extends Controller {

//
protected $table = 'role';
protected $primaryKey = 'id_role';

public function index() {

$list_role = DB::table('role')->get()->toArray();

return view('role.index')->with('list_role', $list_role);
}

}

As it can be seen from the above snippet code extracted from a file named RoleController.php used to define the controller named RoleController, there is no method named ‘create’ which has already defined. So, in order to solve the problem, just create a new method inside the class Controller as shown below :

<?php

namespace App\Http\Controllers;

use Illuminate\Support\Facades\DB;
use Illuminate\Http\Request;
use App\Http\Requests;

class RoleController extends Controller {

//
protected $table = 'role';
protected $primaryKey = 'id_role';

public function index() {

$list_role = DB::table('role')->get()->toArray();

return view('role.index')->with('list_role', $list_role);
}

public function create() {
return view('role.create'); 
}

}

Later on, just create a file named ‘create.blade.php’ inside the role folder which is located normally ‘/resources/views/role/’ as shown in the format below :

user@hostname:/var/www/html/laravel-project$ tree resources/views/role/
resources/views/role/
├── create.blade.php
└── index.blade.php

0 directories, 3 files
user@hostname:/var/www/html/laravel-project$

Leave a Reply