laravel php artisan 路由上的 ReflectionException 错误
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/22868282/
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
ReflectionException error on php artisan routes
提问by Halnex
Everytime I run php artisan routes
, I get ReflectionException
error and that UsersController
class does not exist. I'm pretty sure it does.
每次运行时php artisan routes
,都会ReflectionException
出错,并且UsersController
该类不存在。我很确定确实如此。
I tried php composer dump-autoload
and php artisan clear-compiled
, they all give the same error.
我试过php composer dump-autoload
和php artisan clear-compiled
,他们都给予了同样的错误。
This is my error log in /app/storage/logs/
这是我的错误登录 /app/storage/logs/
[2014-03-30 01:41:24] production.ERROR: exception 'ReflectionException' with message 'Class UsersController does not exist' in C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Routing\ControllerInspector.php:28
Stack trace:
#0 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Routing\ControllerInspector.php(28): ReflectionClass->__construct('UsersController')
#1 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Routing\Router.php(269): Illuminate\Routing\ControllerInspector->getRoutable('UsersController', 'users')
#2 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Support\Facades\Facade.php(211): Illuminate\Routing\Router->controller('users', 'UsersController')
#3 C:\wamp\www\re3\app\routes.php(14): Illuminate\Support\Facades\Facade::__callStatic('controller', Array)
#4 C:\wamp\www\re3\app\routes.php(14): Illuminate\Support\Facades\Route::controller('users', 'UsersController')
#5 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\start.php(269): require('C:\wamp\www\re3...')
#6 [internal function]: {closure}(Object(Illuminate\Foundation\Application))
#7 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(792): call_user_func(Object(Closure), Object(Illuminate\Foundation\Application))
#8 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(569): Illuminate\Foundation\Application->fireAppCallbacks(Array)
#9 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(552): Illuminate\Foundation\Application->bootApplication()
#10 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Console\Application.php(44): Illuminate\Foundation\Application->boot()
#11 C:\wamp\www\re3\vendor\laravel\framework\src\Illuminate\Console\Application.php(33): Illuminate\Console\Application::make(Object(Illuminate\Foundation\Application))
#12 C:\wamp\www\re3\artisan(46): Illuminate\Console\Application::start(Object(Illuminate\Foundation\Application))
#13 {main} [] []
回答by Antonio Carlos Ribeiro
Somehow Laravel is not being able to find your UsersController, run
不知何故,Laravel 无法找到您的 UsersController,运行
composer dumpautoload
Then check the file vendor/composer/autoload_classmap.php
, your UsersController has to be there, otherwise composer won't be able to autoload it and Laravel will not have access to it.
然后检查文件vendor/composer/autoload_classmap.php
,您的 UsersController 必须在那里,否则 composer 将无法自动加载它并且 Laravel 将无法访问它。
if you cannot find your controllers in int, you have to check:
如果在 int 中找不到控制器,则必须检查:
1) Your composer.json file, the folder where your controllers are must be in:
1) 你的 composer.json 文件,你的控制器所在的文件夹必须在:
"autoload": {
"classmap": [
"app/controllers",
....
],
2) Check if your classes are correctly named.
2)检查您的类是否正确命名。
3) If you are using a namespace:
3) 如果您使用的是命名空间:
class UsersController extends Controller { ... }
类 UsersController 扩展控制器 { ... }
You must use the namespace in your references to it and, probably it would be better, in this case, to use PSR-4 (or even PSR-0) to autoload your classes.
您必须在对它的引用中使用命名空间,在这种情况下,最好使用 PSR-4(甚至 PSR-0)来自动加载您的类。
4) Compare classes that you have in autoload_classmap.php with those that are not there. There must be a difference in naming or directory placement.
4) 将 autoload_classmap.php 中的类与不存在的类进行比较。命名或目录放置必须有所不同。
5) Check if your classes all start with
5)检查您的课程是否都以
<?php
and not just
而不仅仅是
<?
This may not make too much difference for composer nor PHP, but it does for Laravel.
这对于 composer 和 PHP 可能没有太大区别,但对于 Laravel 来说却是不同的。
回答by Satheesh kumar
In the controller file check the namespace.
在控制器文件中检查命名空间。
For example,
例如,
namespace App\Http\Controllers\home;
In the name space above the controller file should exist in the home folder. If it exists in the home folder and if I have not included home above it give an reflection error.
在上面的命名空间中,控制器文件应该存在于主文件夹中。如果它存在于 home 文件夹中,并且我没有在上面包含 home,则会出现反射错误。
Simply put the name space should correspond the folder structure where the controller file is placed.
简单地把命名空间应该对应于放置控制器文件的文件夹结构。
回答by Slava
In my case it happens that I had two controllers with the same name but in different locations:
就我而言,碰巧我有两个名称相同但位于不同位置的控制器:
app\Http\Controllers\Auth\PasswordController.php
app\Http\Controllers\Auth\PasswordController.php
and
和
app\Http\Controllers\Admin\Auth\PasswordController.php
app\Http\Controllers\Admin\Auth\PasswordController.php
The last one
最后一个
app\Http\Controllers\Admin\Auth\PasswordController.php
app\Http\Controllers\Admin\Auth\PasswordController.php
had a wrong namespace (namespace App\Http\Controllers\Auth;instead of **namespace App\Http\Controllers\Admin\Auth;*) and because of this I constantly get the ReflectionExceptionexception
有一个错误的命名空间(命名空间 App\Http\Controllers\Auth;而不是 **namespace App\Http\Controllers\Admin\Auth;*),因此我不断收到ReflectionException异常
回答by Bannakon
check Controller name sometimes you may forget 's' , such as StudentsController
检查控制器名称有时您可能会忘记 's' ,例如 StudentsController