如何避免 Laravel 项目中的反射异常以及产生它的原因是什么?

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

How to avoid Reflection Exception in Laravel's project and what are the causes which produce it?

phplaravellaravel-5laravel-routing

提问by Raj

I have made a project named my-schoolusing Laravel. Everything works fine, except when I run my project using XAMPP.
I'm then using this url: localhost/my-school/public/.

我制作了一个my-school使用 Laravel命名的项目。一切正常,除非我使用 XAMPP 运行我的项目。
然后我使用这个 url: localhost/my-school/public/

Here's the raised exception:

这是引发的异常:

Fatal error: Uncaught exception 'ReflectionException' with message
'Class MySchool\Http\Kernel does not exist' in
E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Container\Container.php:776
Stack trace: #0
E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Container\Container.php(776):
ReflectionClass->__construct('MySchool\Http\K...') #1
E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Container\Container.php(656):
Illuminate\Container\Container->build('MySchool\Http\K...', Array)
#2 E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Foundation\Application.php(613):
Illuminate\Container\Container->make('MySchool\Http\K...', Array) #3
E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Container\Container.php(229):
Illuminate\Foundation\Application->make('MySchool\Http\K...', Array)
#4 E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Container\Container.php(773):
Illuminate\Container\Container->Illuminate\Container\{clo in
E:\xampp\htdocs\my-school\vendor\laravel\framework\src\Illuminate\Container\Container.php
on line 776

And here is my composer.jsonfile content:

这是我的composer.json文件内容:

{
    "name": "laravel/laravel",
    "description": "The Laravel Framework.",
    "keywords": ["framework", "laravel"],
    "license": "MIT",
    "type": "project",
    "require": {
        "laravel/framework": "5.0.*"
    },
    "require-dev": {
        "phpunit/phpunit": "~4.0",
        "phpspec/phpspec": "~2.1"
    },
    "autoload": {
        "classmap": [
            "database"
        ],
        "psr-4": {
            "App\": "app/"
        }
    },
    "autoload-dev": {
        "classmap": [
            "tests/TestCase.php"
        ]
    },
    "scripts": {
        "post-install-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-update-cmd": [
            "php artisan clear-compiled",
            "php artisan optimize"
        ],
        "post-create-project-cmd": [
            "php -r \"copy('.env.example', '.env');\"",
            "php artisan key:generate"
        ]
    },
    "config": {
        "preferred-install": "dist"
    }
}

Can somebody please help me finding an issue with my project?

有人可以帮我找到我的项目的问题吗?

采纳答案by manix

Take a look to the composer autoload's value:

看看 Composer 自动加载的值:

"autoload": {
    "classmap": [
        "database"
    ],
    "psr-4": {
        "App\": "app/"
    }
},

This means that your namespace for all classes inside of app\ foldershould be start with 'App\' as name. In your case, the error states:

这意味着您的所有类的命名空间都app\ folder应该以“App\”作为名称开头。在您的情况下,错误指出:

Fatal error: Uncaught exception 'ReflectionException' with message 'Class MySchool\Http\Kernel does not exist'

致命错误:未捕获的异常 'ReflectionException' 带有消息 'Class MySchool\Http\Kernel 不存在'

MySchool\Http\Kernelwill never be loaded because it does not exist. Instead, your composer demands the following structure:

MySchool\Http\Kernel永远不会被加载,因为它不存在。相反,您的作曲家需要以下结构:

Class App\Http\Kernel

See the difference? then, try to rename the app as follow:

看到不同?然后,尝试按如下方式重命名应用程序:

php artisan app:name MySchool 

At that way, all classes will route as MySchool as I think you want.

这样,所有课程都将按照我认为的方式作为 MySchool 进行路由。