Laravel 5.5-找不到类自定义BaseController但存在 [英] Laravel 5.5 - Class Custom BaseController not found but exists

查看:85
本文介绍了Laravel 5.5-找不到类自定义BaseController但存在的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Laravel 5.5自定义BaseController找不到,即使它存在.已经检查了StackOverflow上与找不到的BaseController有关的其他问题,但它们是指默认的BaseController,在我的情况下是不相同的.

Laravel 5.5 Custom BaseController not found even though it exists. Have checked the other questions on StackOverflow regarding the BaseController not found but they are referring to the default BaseController which isn't the same in mine case.

这是我的实现方式

use Illuminate\Foundation\Bus\DispatchesJobs;
use Illuminate\Routing\Controller as CheckController;
use Illuminate\Foundation\Validation\ValidatesRequests;
use Illuminate\Foundation\Auth\Access\AuthorizesRequests;

class Controller extends CheckController
{
    use AuthorizesRequests, DispatchesJobs, ValidatesRequests;


}

自定义BaseController(BaseController.php)

use App\Http\Controllers\Controller;

class BaseController extends Controller
{

    /**
     * Setup the layout used by the controller.
     *
     * @return void
     */
    public $data = array();

    public function __construct()
    {
        if (Sentinel::check()) {
            // User is not logged in, or is not activated
            $this->data['admin'] = Sentinel::getUser();
        }
    }

    protected function setupLayout()
    {
        if (!is_null($this->layout)) {
            $this->layout = View::make($this->layout);
        }
    }

}

将名为HomeCtontroller的类扩展到自定义BaseController

Extending a class named HomeCtontroller to Custom BaseController

class HomeController extends BaseController {

    protected $layout = 'master';

    public function main()
    {
         ...
    }

}

然后给出以下错误

Symfony \ Component \ Debug \ Exception \ FatalErrorException (E_UNKNOWN)
Class 'BaseController' not found

将感谢各种指针.

推荐答案

我相信您还没有包含完整的名称空间.

I believe you haven't included full namespaces.

确保在BaseController中具有:

Make sure in BaseController you have:

namespace App\Http\Controllers;

并在HomeController中确保您正在使用:

and in HomeController make sure you are using:

use App\Http\Controllers\BaseController;

所有这些控制器都应位于app/Http/Controllers目录中.

all those controllers should be located in app/Http/Controllers directory.

如果确定您具有有效的目录和名称空间,请在控制台中运行composer dump-autoload

If you are sure you have valid directories and namespaces run composer dump-autoload in console

这篇关于Laravel 5.5-找不到类自定义BaseController但存在的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆