Controller does not exist in Laravel 5.3

Posted on

This is an article which is showing that a controller does not exist when the actual controller is already there in the development of application specifically a web-based application using Laravel 5.3 as its main framework of development.

Basically there is an image which can be used as a prove of the above situation which is shown below :

Controller does not exist Laravel 5.3
Controller does not exist Laravel 5.3

The above is an error which can be also identified from the following laravel.log file located in storage/logs as shown below :

[2017-02-12 15:51:28] local.ERROR: ReflectionException: Class App\Http\Controllers\ServerController does not exist in /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php:748
Stack trace:
#0 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php(748): ReflectionClass->__construct('App\\Http\\Contro...')
#1 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Container/Container.php(643): Illuminate\Container\Container->build('App\\Http\\Contro...', Array)
#2 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Application.php(709): Illuminate\Container\Container->make('App\\Http\\Contro...', Array)
#3 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(203): Illuminate\Foundation\Application->make('App\\Http\\Contro...')
#4 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(316): Illuminate\Routing\Route->getController()
#5 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Route.php(278): Illuminate\Routing\Route->controllerMiddleware()
#6 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(655): Illuminate\Routing\Route->gatherMiddleware()
#7 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(635): Illuminate\Routing\Router->gatherRouteMiddleware(Object(Illuminate\Routing\Route))
#8 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(618): Illuminate\Routing\Router->runRouteWithinStack(Object(Illuminate\Routing\Route), Object(Illuminate\Http\Request))
#9 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Router.php(596): Illuminate\Routing\Router->dispatchToRoute(Object(Illuminate\Http\Request))
#10 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(267): Illuminate\Routing\Router->dispatch(Object(Illuminate\Http\Request))
#11 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(53): Illuminate\Foundation\Http\Kernel->Illuminate\Foundation\Http\{closure}(Object(Illuminate\Http\Request))
#12 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Middleware/CheckForMaintenanceMode.php(46): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#13 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(137): Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode->handle(Object(Illuminate\Http\Request), Object(Closure))
#14 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Routing/Pipeline.php(33): Illuminate\Pipeline\Pipeline->Illuminate\Pipeline\{closure}(Object(Illuminate\Http\Request))
#15 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Pipeline/Pipeline.php(104): Illuminate\Routing\Pipeline->Illuminate\Routing\{closure}(Object(Illuminate\Http\Request))
#16 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(149): Illuminate\Pipeline\Pipeline->then(Object(Closure))
#17 /var/www/html/laravel-project/vendor/laravel/framework/src/Illuminate/Foundation/Http/Kernel.php(116): Illuminate\Foundation\Http\Kernel->sendRequestThroughRouter(Object(Illuminate\Http\Request))
#18 /var/www/html/laravel-project/public/index.php(53): Illuminate\Foundation\Http\Kernel->handle(Object(Illuminate\Http\Request))
#19 {main}  

The above error specifically pointed out an error as follows :

Class App\Http\Controllers\ServerController does not exist

This is an attempt to locate the file which is normally for a Laravel-based web application’s controller is in ‘app\Http\Controller’ :

user@hostname:/var/www/html/laravel-project/app/Http/Controllers$ ls -al | grep ServerController.php 
-rw-rw-r-- 1 user user 1703 Feb 13 15:05 ServerController.php user@hostname:/var/www/html/laravel-project/app/Http/Controllers$

So, what seems to be the problem when the file named ‘ServerController.php’ exist but the existence is not acclaimed. After checking the content of the file, there is no consistency between the name of the class with the name of the file as shown below :

class ServerCRUDController extends Controller
{
#    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


        /**
         *  Display listing of Server
         */

        public function index(Request $request)
        {
                $servers = Server::orderBy('id','DESC')->paginate(5);
                return view('server.index',compact('servers'))
                        ->with('i', ($request->input('page',1) - 1) * 5);
        }

The class name is ‘ServerCRUDController’ and the file name is ‘ServerController’. So, in order to solve the problem, just change the class name from ‘ServerCRUDController’ into ‘ServerController’ in the following line of code :

class ServerCRUDController extends Controller

into this following line of code :

class ServerController extends Controller

Re-execute the script and if nothing else goes wrong, the above error will be actually taken cared of.

One thought on “Controller does not exist in Laravel 5.3

Leave a Reply