Class Controller not found in laravel 5

27,596

Solution 1

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;

Solution 2

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.

Share:
27,596
gospelslide
Author by

gospelslide

Computer engineering student,I love food,music & coding.

Updated on July 09, 2022

Comments

  • gospelslide
    gospelslide almost 2 years

    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?

  • gospelslide
    gospelslide over 8 years
    I tried that and the controller works,but now as the namespace is the controller directory,I get an error view not found.Any other options
  • Limon Monte
    Limon Monte over 8 years
    @gospelslide you can either add use View; to the top of controller or use alias view like this: return view('welcome')->with('name', $name);