Laravel 4无法从命名空间控制器中找到BaseController [英] Laravel 4 Can't find BaseController from namespaced controller

查看:254
本文介绍了Laravel 4无法从命名空间控制器中找到BaseController的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

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:

/ [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

默认的laravel登陆页面可以正常加载.但是,当我尝试使用自己的名称空间设置控制器时,我总是收到错误消息.

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.

这是 HomeController.php :

namespace PageTypes;

use Home\controllers;

class HomeController extends BaseController {

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

}

这是 routes.php :

<?php

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

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

此设置产生错误:"Symfony \组件\调试\异常\ FatalErrorException 找不到类'PageTypes \ BaseController'.好的,laravel至少找到了HomeController.php.

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

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

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

我不了解名称空间,psr-0和laravel路由的交集,对您的任何帮助将不胜感激.

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

后续问题:(1)我应该采取什么步骤来调试它? NGINX的日志并没有真正告诉我任何东西,除了抛出异常时我所看到的. (2)有没有人在github上遇到过类似布局的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.

这是我的配置设置:

// 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"
}

推荐答案

class HomeController extends \BaseController {

基本上,因为您在名称空间中,所以需要在全局名称空间中解析BaseController. (这就是\的作用.)

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

您也可以摆脱

use \BaseController;

class HomeController extends BaseController {

但是由于您将只在整个控制器中一次引用BaseController,所以没有什么意义.

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

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

The other potential problem is your namespacing which is what PageTypes\Home\controllers\HomeController is 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).

尝试使用

namespace PageTypes\Home;

而不只是PageTypes

这篇关于Laravel 4无法从命名空间控制器中找到BaseController的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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