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

北城以北 提交于 2019-12-20 02:06:22

问题


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

Route.php

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

templateController.blade.php

class templateController extends BaseController {

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


}

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

================= 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

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


回答1:


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

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

  1. Create a new file in app/controllers named TemplateController.php
  2. Open up terminal and run composer dumpautoload

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




回答2:


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 blade in a Blade view, i.e: myView.blade.php basically stored in app/views/ folder. Read more about blate template on Laravel website.




回答3:


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

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

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

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

class TemplateController extends BaseController {

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

}

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

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

Now it should all work.



来源:https://stackoverflow.com/questions/24206342/laravel-controller-doesnt-exist-even-though-it-clearly-exists

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