Class Controller not found in laravel 5
Asked Answered
N

7

7

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?

Newton answered 14/11, 2015 at 17:44 Comment(1)
Make sure your controller is namespaced as wellQuentinquercetin
V
10

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;
Vegetable answered 14/11, 2015 at 17:49 Comment(2)
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 optionsNewton
@Newton you can either add use View; to the top of controller or use alias view like this: return view('welcome')->with('name', $name);Vegetable
R
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.

Royster answered 14/11, 2015 at 17:47 Comment(0)
C
1

My issue was a different class name than the one in the class that extends controller, the names should be same

Comus answered 11/11, 2017 at 9:43 Comment(0)
M
1

If you are creating a controller inside a subfolder with a different namespace then use this on your controller file:

use App\Http\Controllers\Controller;
Metameric answered 21/10, 2020 at 7:56 Comment(0)
C
1

In your routes/web.php, use the namespace for the PagesController eg.

<?php
  use App\Http\Controllers\Controller\PagesController;

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

This will enable you to access the PagesController class.

Cypher answered 27/4, 2023 at 6:11 Comment(0)
J
0

I had a similar issue, where I could not use controllers I created, it worked after i used the controller with its full path:

Route::get('/pizzas/{id}', [App\Http\Controllers\PizzaController::class, 'show']);

the use function at the top of the web.php file did not work

Jerz answered 23/12, 2023 at 9:1 Comment(0)
E
0

If you're getting a 404 error for your controllers in Laravel 11, try running:

php artisan optimize

This command clears and rebuilds your cached files. It often resolves routing issues.

Eric answered 25/7 at 10:47 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.