未找到 Laravel 类 BaseController

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/31791729/
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:06:39  来源:igfitidea点击:

Laravel Class BaseController not found

phplaravel

提问by Evaldas L.

I'm newbie in laravel. So I've created TestController.php file, inside file:

我是laravel的新手。所以我在文件里面创建了 TestController.php 文件:

<?php
class TestController extends BaseController {
public function testFunction()
{
    return "It works";
}
}

In routes:

在路线中:

Route::get('test', 'TestController@testFunction');

I'm got this error:

我有这个错误:

FatalErrorException in TestController.php line 2: Class 'BaseController' not found

TestController.php 第 2 行中的 FatalErrorException:找不到类“BaseController”

I'm using laravel 5, I'm trying to change from BaseController to Controller, not works :( What's wrong here? Thanks in advance :(

我正在使用 laravel 5,我正在尝试从 BaseController 更改为 Controller,但不起作用:( 这里有什么问题?提前致谢:(

P.S I'm tried change to "\BaseController". Not works.

PS 我尝试更改为“\BaseController”。不行。

回答by baao

There is no such thing as a BaseControllerin Laravel, use Controllerinstead. BaseControlleris used once throughout the framework, in the Controllerclass, but only as a use as alias:

BaseControllerLaravel 中没有 a 之类的东西,请Controller改用。BaseController在整个框架的Controller类中使用一次,但仅用作别名:

<?php

namespace App\Http\Controllers;

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as BaseController;  // <<< See here - no real class, only an alias
use Illuminate\Foundation\Validation\ValidatesRequests;

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

Your controller needs this:

你的控制器需要这个:

<?php namespace App\Http\Controllers;

class TestController extends Controller 
{
    public function testFunction()
    {
        return "It works";
    }
}

Is your TestController in app/Http/Controllers?

你的 TestController 在 app/Http/Controllers 中吗?

enter image description here

在此处输入图片说明