Laravel 控制器不存在,即使它明显存在

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

Laravel Controller doesn't exist, even though it clearly exists

phplaravellaravel-4

提问by jamadri

The error I'm getting is that the controller doesn't exist even though I know it does, here's the code.

我得到的错误是控制器不存在,即使我知道它存在,这是代码。

Route.php

路由.php

Route::get('mdpay/template', array("uses" => "templateController@index"));

templateController.blade.php

模板控制器.blade.php

class templateController extends BaseController {

  public function index()
  {
    echo "made it";
  }


}

Why might I be getting this error: Class TemplateController does not exist

为什么我会收到此错误:Class TemplateController 不存在

================= UPDATE: ==================

================ 更新:==================

Ok, so I've created the correct route, renamed my file, and corrected the class name and I'm still coming up with that error.

好的,所以我创建了正确的路由,重命名了我的文件,并更正了类名,但我仍然想出了那个错误。

File Names:

文件名:

templateController.php

模板控制器.php

// File Name: TemplateController.php
class TemplateController extends BaseController {
    public function index()
    {
        // app/views/myView.blade.php
        echo "hello";
    }
}

My route is:

我的路线是:

Route::get('mdpay/template', array("uses" => "TemplateController@index"));

Still receiving Controller Doesn't Exist error. All my other controllers (3 others) are working except this one.

仍然收到控制器不存在错误。除了这个控制器之外,我的所有其他控制器(另外 3 个)都在工作。

回答by Marwelln

If you are using the standard composer classmap autoloader you need to composer dumpautoloadeverytime you create a new file.

如果您使用标准的 Composer 类映射自动加载器,则composer dumpautoload每次创建新文件时都需要这样做。

So to create a new controller with the standard composer setup given by Laravel:

因此,要使用 Laravel 提供的标准 Composer 设置创建一个新控制器:

  1. Create a new file in app/controllersnamed TemplateController.php
  2. Open up terminal and run composer dumpautoload
  1. app/controllers命名中创建一个新文件TemplateController.php
  2. 打开终端并运行 composer dumpautoload

As previous users have said, only view files should end with .blade.php.

正如之前的用户所说,只有视图文件应该以.blade.php.

回答by The Alpha

It should be:

它应该是:

// File Name: TemplateController.php
class TemplateController extends BaseController {
    public function index()
    {
        // return "made it"; // or
        // app/views/myView.blade.php
        return View::make('myView');
    }
}

Route for that:

路线:

Route::get('mdpay/template', array("uses" => "TemplateController@index"));

Use bladein a Blade view, i.e: myView.blade.phpbasically stored in app/views/folder. Read more about blate templateon Laravelwebsite.

blade在 a 中使用Blade view,即:myView.blade.php基本上存储在app/views/文件夹中。在网站上阅读有关blate 模板的更多信息Laravel

回答by ollieread

Controllers live in the app/controllersdirectory and should remain there unless you have your own namespaced structure.

控制器位于app/controllers目录中,除非您有自己的命名空间结构,否则应保留在那里。

The reason you're getting a Class TemplateController does not existis because it doesn't, firstly, your class is called templateControllerand secondly, it exists as templateController.blade.phpwhich wouldn't be loaded in this way.

你得到 a 的原因Class TemplateController does not exist是因为它没有,首先,你的类被调用templateController,其次,它存在,因为templateController.blade.php它不会以这种方式加载。

Blade files are for views, and only views within app/viewsor a custom views directory should end with .blade.php.

Blade 文件用于视图,只有视图app/views或自定义视图目录中的视图应以.blade.php.

Create the file app/controllers/TemplateController.phpand add the following code to it.

创建文件app/controllers/TemplateController.php并向其中添加以下代码。

class TemplateController extends BaseController {

    public function index()
    {
        echo "made it";
    }

}

Now on the command line, run the command composer dumpautoloadand change you route declaration to:

现在在命令行上,运行命令composer dumpautoload并将路由声明更改为:

Route::get('mdpay/template', array('uses' => 'TemplateController@index"));

Now it should all work.

现在它应该一切正常。