简体   繁体   English

找不到Laravel命名空间类

[英]Laravel namespaces class not found

I am newbie to Laravel namespaces. 我是Laravel命名空间的新手。

I am trying to do something like this: 我正在尝试做这样的事情:

namespace App\Controllers; // when I remove this line, everything works fine... I need to include this

class HomeController extends BaseController {

    protected $layout = "layouts.main";

    public function __construct() {
        // some stuff here
    }

    /**
     * Home page.
     * @return View
     */
    public function getHome() {
        // Show the page
        $this->layout->content = View::make('home');
    }
}

But I am having this weird error, 但是我有这个奇怪的错误,

Class HomeController does not exist 

Here is some of my composer.json stuff, 这是我的一些composer.json内容,

"autoload": {
    "classmap": [
        "app/commands",
        "app/controllers",
        "app/libraries",            
        "app/models",
        "app/database/migrations",
        "app/database/seeds",
    ]
}, 

I have also executed, 我也执行过

composer dump-autoload

While I am routing something like this, 当我路由这样的内容时,

# Default
Route::get('/', array('as' => 'home', 'uses' => 'HomeController@getHome'));

Here's one common case where this error can occur: 这是可能发生此错误的一种常见情况:

Imagine you're defining a route using that class: 假设您正在使用该类定义路线:

Route::get('/home', 'HomeController@getIndex');

The Laravel core is going to pass that string ( 'HomeController@getIndex' ) to some method(s) deep in the bowels of the routing class, parse it, instantiate HomeController , and call getIndex . Laravel核心将把该字符串( 'HomeController@getIndex' )传递给路由类深处的某些方法,对其进行解析,实例化HomeController并调用getIndex Does that code include a use App\\Controllers directive? 该代码是否包含use App\\Controllers指令? Not likely, since this is something you've created. 不太可能,因为这是您创建的。 Basically, wherever that HomeController class is used (and I have no idea where it is), the PHP interpreter is not going to know where that class comes from. 基本上,无论在哪里使用HomeController类(我都不知道它在哪里),PHP解释器都不会知道该类的来源。

The solution is to use the fully-qualified class name. 解决方案是使用完全限定的类名。 That means including the full namespace in the string, like so: 这意味着在字符串中包括完整的名称空间,如下所示:

Route::get('/home', '\App\Controller\HomeController@getIndex');

Now when Laravel tries to instantiate the class, it has everything it needs to find it. 现在,当Laravel尝试实例化该类时,它具有找到它所需的一切。

I don't know for sure if this is the problem - you need to show us the code where the error occurs - but this is one possibility. 我不确定这是否是问题所在-您需要向我们显示发生错误的代码-但这是一种可能。

Are you typing use App\\Controllers\\HomeController in the file where you're trying to use it? 您要在尝试使用的文件中键入use App\\Controllers\\HomeController吗? This essentially includes it. 这基本上包括它。

You don't use namespaces for Controllers in app/controllers. 您不在应用程序/控制器中为控制器使用名称空间。 HomeController extends BaseController which extends Controller in the framework. HomeController扩展了BaseController,后者扩展了框架中的Controller。

You might want to use namespaces if you are including custom libraries into the framework. 如果要将自定义库包含在框架中,则可能要使用名称空间。 /app/libraries for example. / app / libraries。

As long as your routes.php has some url to get to a home controller method, it should work. 只要您的routes.php有一些到达家用控制器方法的URL,它就应该起作用。

Route::get('/home', 'HomeController@index');

HomeController.php: HomeController.php:

class HomeController extends BaseController {

    private $myVar;

    public function __construct() {
        // some stuff here
    }

    public function index(){
        return View::make('home');
    }
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM