Laravel 5.5 - 未找到类自定义BaseController但存在

时间:2017-11-19 19:05:05

标签: php laravel laravel-5 namespaces laravel-5.5

Laravel 5.5自定义BaseController即使存在也找不到。已检查StackOverflow上有关未找到BaseController的其他问题,但他们指的是默认BaseController,但在我的案例中并不相同。

这是我的实施

Controller.php这样

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

class HomeController extends BaseController {

    protected $layout = 'master';

    public function main()
    {
         ...
    }

}

然后它会出现以下错误

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

会欣赏任何类型的指示。

1 个答案:

答案 0 :(得分:1)

我相信你还没有包含完整的命名空间。

在BaseController中确保你有:

namespace App\Http\Controllers;

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

use App\Http\Controllers\BaseController;

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

如果您确定在控制台

中运行composer dump-autoload有效目录和命名空间