Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Class Controller not found in laravel 5

So I am new to laravel and was just trying out some code to clear the basics,but however after creating a new controller to handle a route.It throws a fatal exception saying Class 'Controller' not found!

(The controller.php exists in the same directory)

The controller.php code is the default one

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

abstract class Controller extends BaseController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;
}

This is my PagesController.php code

<?php

class PagesController extends Controller
{
    public function home()
    {
        $name = 'Yeah!';
        return View::make('welcome')->with('name',$name);
    }
}

This is route.php code

<?php

Route::get('/','PagesController@home');

The welcome.blade.php code is the default one except it displays the variable $name instead of laravel 5. What am I getting wrong?

like image 629
gospelslide Avatar asked Nov 14 '15 17:11

gospelslide


2 Answers

When you reference a class like extends Controller PHP searches for the class in your current namespace.

In this case that's a global namespace. However the Controller class obviously doesn't exists in global namespace but rather in App\Http\Controllers.

You need to specify the namespace at the top of PagesController.php:

namespace App\Http\Controllers;
like image 176
Limon Monte Avatar answered Sep 19 '22 01:09

Limon Monte


You will want to specify the namespace to your Controller class:

class PagesController extends \App\Http\Controllers\Controller

otherwise Controller is looked up in the default root namespace \, where it does not exist.

like image 42
Kenney Avatar answered Sep 19 '22 01:09

Kenney