在 Laravel 4 中路由到控制器失败

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

Routing to Controller fails in Laravel 4

phplaravel

提问by Matanya

I'm trying to register my controllers in Laravel 4. In routes.phpI have:

我正在尝试在 Laravel 4 中注册我的控制器。routes.php我有:

Route::get("/","HomeController@index");
Route::get("search","SearchController@index");

Now, the home controller route is fine, but the search controller route gives me an error:

现在,家庭控制器路由很好,但搜索控制器路由给我一个错误:

ReflectionException: Class SearchController does not exist

However, the class doesexist. I even tried to create another sample controller, but to no avail, as the same message came up.

但是,该类确实存在。我什至尝试创建另一个示例控制器,但无济于事,因为出现了相同的消息。

回答by BenG

names are case sensitive in L4. And the method name should match exactly(e.g. getIndex, not index() ).

L4 中的名称区分大小写。并且方法名称应该完全匹配(例如 getIndex,而不是 index() )。

Also, because it uses composer packages, you will need to run: php composer dump-autoload to detect any new classes/controllers

此外,因为它使用作曲家包,你需要运行: php composer dump-autoload 检测任何新的类/控制器

回答by eaykin

I had the same problem and composer dump-autoloaddid not resolve it.

我有同样的问题,作曲家dump-autoload没有解决它。

I realized that my class file was not declared in vendor/composer/autoload_classmap.php generated by Composer.

我意识到我的类文件没有在 Composer 生成的 vendor/composer/autoload_classmap.php 中声明。

I cleared the contents of the cache folder. In Linux, this folder is: ~/.composer/cache and in Windows 7: C:\Users\\AppData\Local\Composer\files If bootstrap/compiled.php is present, it should also be removed, or run: php artisan clear-compiled.

我清除了缓存文件夹的内容。在 Linux 中,这个文件夹是:~/.composer/cache,在 Windows 7 中:C:\Users\\AppData\Local\Composer\files 如果 bootstrap/compiled.php 存在,它也应该被删除,或者运行:php artisan clear-compiled

After this, I removed composer.lock and the vendor folder and run: composer install. This put my class file into autoload_classmap.php however the ReflectionException was still being thrown...

在此之后,我删除了 composer.lock 和供应商文件夹并运行:composer install。这将我的类文件放入 autoload_classmap.php 但是 ReflectionException 仍然被抛出......

As a final try, I copied and renamed another controller from the project that was already in use, and this resolved the problem.

作为最后的尝试,我从已经在使用的项目中复制并重命名了另一个控制器,这解决了问题。