phplaravellaravel-5laravel-routing

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?


Solution

  • 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;