Laravel 4 Can't find BaseController from namespaced controller

大兔子大兔子 提交于 2020-01-23 11:44:14

问题


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

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.

This is HomeController.php:

namespace PageTypes;

use Home\controllers;

class HomeController extends BaseController {

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

}

Here's 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.

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

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

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.

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

回答1:


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

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.

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

Try using

namespace PageTypes\Home;

instead of just PageTypes



来源:https://stackoverflow.com/questions/18687464/laravel-4-cant-find-basecontroller-from-namespaced-controller

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!