在 Laravel 5 中找不到类控制器

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/33711379/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 12:41:35  来源:igfitidea点击:

Class Controller not found in laravel 5

phplaravellaravel-5laravel-routing

提问by gospelslide

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!

所以我是 laravel 的新手,只是尝试了一些代码来清除基础知识,但是在创建一个新的控制器来处理路由之后。它抛出一个致命的异常,说找不到类“控制器”!

(The controller.php exists in the same directory)

(controller.php 存在于同一目录下)

The controller.php code is the default one

controller.php 代码是默认的

<?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

这是我的 PagesController.php 代码

<?php

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

This is route.php code

这是 route.php 代码

<?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?

Welcome.blade.php 代码是默认代码,除了它显示变量 $name 而不是 laravel 5。我哪里出错了?

回答by Limon Monte

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

当您引用像extends ControllerPHP这样的类时,会在您当前的命名空间中搜索该类。

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

在这种情况下,这是一个全局命名空间。然而,Controller该类显然不存在于全局命名空间中,而是存在于App\Http\Controllers.

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

您需要在顶部指定命名空间PagesController.php

namespace App\Http\Controllers;

回答by Kenney

You will want to specify the namespace to your Controllerclass:

您需要为您的Controller类指定命名空间:

class PagesController extends \App\Http\Controllers\Controller

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

否则Controller在默认根命名空间中查找\,它不存在。

回答by user2638733

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

我的问题是与扩展控制器的类中的类名不同,名称应该相同