Laravel 4 无法从命名空间控制器中找到 BaseController

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

Laravel 4 Can't find BaseController from namespaced controller

laravellaravel-4psr-0

提问by swaysteady

I'm trying to structure my Laravel 4 site such that (1) major application groups' components (controllers/views/etc) are coupled together and (2) Laravel's supporting code outside of in my web server's document root. The default laravel home page loads fine, but I can't get a namespaced controller to route correctly. This is the relevant file structure:

我正在尝试构建我的 Laravel 4 站点,以便 (1) 主要应用程序组的组件(控制器/视图/等)耦合在一起,以及 (2) Laravel 的支持代码在我的 Web 服务器的文档根目录之外。默认的 Laravel 主页加载正常,但我无法让命名空间控制器正确路由。这是相关的文件结构:

/ [Project Root]
     /laravel [full laravel install here]
          composer.json
          /app
               /controllers
                    BaseController.php
     /dev
          /htdocs
               index.php
               /app
                    /PageTypes
                         /Home
                              /controllers
                                   HomeController.php
                              /views
                                   HomeView.blade.php

The default laravel landing page is loading fine. But when I tried setting up a controller with its own namespace, I keep getting the errors.

默认的 Laravel 登陆页面加载良好。但是当我尝试使用自己的命名空间设置控制器时,我不断收到错误消息。

This is HomeController.php:

这是HomeController.php

namespace PageTypes;

use Home\controllers;

class HomeController extends BaseController {

    public function showWelcome()
    {
        return View::make('hello');
    }

}

Here's routes.php:

这是routes.php

<?php

Route::get('/', function()
{
    return View::make('hello');
});

Route::get('/home', 'PageTypes\Home\controllers\HomeController@showWelcome' );

This setup yields the error: "Symfony \ Component \ Debug \ Exception \ FatalErrorException Class 'PageTypes\BaseController' not found" Ok, laravel is finding HomeController.php at least.

这个设置会产生错误:“Symfony \ Component \ Debug \ Exception \ FatalErrorException Class 'PageTypes\BaseController' not found” 好的,laravel 至少找到了 HomeController.php。

Plenty of other SO responses told me to try changing BaseController to \BaseController. Making that one change and leaving everything else the same yielded the error "ReflectionException: Class PageTypes\Home\controllers\HomeController does not exist." What the?... >.<

许多其他 SO 响应告诉我尝试将 BaseController 更改为 \BaseController。进行这一更改并保持其他所有内容不变会产生错误“ReflectionException:Class PageTypes\Home\controllers\HomeController 不存在”。什么?...>.<

I'm not understanding something at the intersection of namespaces, psr-0, and laravel's routing, any help would be REALLY appreciated.

我不了解命名空间、psr-0 和 laravel 路由的交叉点,任何帮助将不胜感激。

Followup questions: (1) what steps could I have taken to have debugged this? NGINX's logs aren't really telling me anything other than what I see in the exception errors being thrown. (2) Has anyone come across a laravel seed on github that's laid out similarly? I'd love to have something to reference.

后续问题:(1)我可以采取哪些步骤来调试它?除了我在抛出的异常错误中看到的内容之外,NGINX 的日志并没有真正告诉我任何其他信息。(2) 有没有人在github上遇到过类似布局的laravel种子?我很想有一些东西可以参考。

Here are my configuration settings:

这是我的配置设置:

// index.php 
...
require __DIR__.'/../../laravel/bootstrap/autoload.php';
$app = require_once __DIR__.'/../../laravel/bootstrap/start.php';
...

// bootstrap/autoload.php
...
require __DIR__.'/../vendor/autoload.php';
...


// bootstrap/paths.php
...
'app' => __DIR__.'/../app',
'public' => __DIR__.'/../../dev/htdocs/',
'base' => __DIR__.'/..',
'storage' => __DIR__.'/../app/storage',
...

// compose.json
{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "require": {
        "laravel/framework": "4.0.*"
    },
    "autoload": {
        "psr-0": {
            "PageTypes": "../dev/htdocs/app"
        },
        "classmap": [
            "app/commands",
            "app/controllers",
            "app/models",
            "app/database/migrations",
            "app/database/seeds",
            "app/tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan optimize"
        ],
        "pre-update-cmd": [
            "php artisan clear-compiled"
        ],
        "post-update-cmd": [
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    },
    "minimum-stability": "dev"
}

回答by Hailwood

class HomeController extends \BaseController {

Basically because you are in a namespace you need to resolve the BaseController in the global namespace(That's what the \does).

基本上因为你在一个命名空间中,你需要在全局命名空间中解析 BaseController (这就是它的\作用)。

You could also get away with having

你也可以逃脱

use \BaseController;

class HomeController extends BaseController {

But since you are only going to be referring to BaseController once in the entire controller there's not much point.

但是由于您只会在整个控制器中引用 BaseController 一次,因此没有多大意义。

The other potential problem is your namespacing which is what PageTypes\Home\controllers\HomeControlleris referring to. That's not actually the Pathname, thats the namespace name it's looking for (it just so happens that the autoloaders that follow PSR match the directory structure to the namespaces).

另一个潜在的问题是您PageTypes\Home\controllers\HomeController所指的命名空间。这实际上不是路径名,而是它要查找的名称空间名称(碰巧跟随 PSR 的自动加载器将目录结构与名称空间相匹配)。

Try using

尝试使用

namespace PageTypes\Home;

instead of just PageTypes

而不仅仅是 PageTypes