Laravel:ReflectionException - 类App \ Http \ Controllers \ XXXX不存在

I am trying to run test page using Laravel.

When I use Controller, every time I got message:

Laravel: ReflectionException - Class App\Http\Controllers\XXXX does not exist

Does somebody knows where problem is?

This is my routes/web.php:

   Route::get('/hi', 'HiController@index');

HiController.php (it is in correct folder structure: app/Http/Controllers/

namespace App\Http\Controllers;

use Illuminate\Http\Request;

class HiController extends Controller
{
    public function index(){
      return "test";
    }
}

RouteServiceProvider.php

namespace App\Providers;

use Illuminate\Support\Facades\Route;
use Illuminate\Foundation\Support\Providers\RouteServiceProvider as ServiceProvider;

class RouteServiceProvider extends ServiceProvider
{
    /**
     * This namespace is applied to your controller routes.
     *
     * In addition, it is set as the URL generator's root namespace.
     *
     * @var string
     */
    protected $namespace = 'App\Http\Controllers';

    /**
     * Define your route model bindings, pattern filters, etc.
     *
     * @return void
     */
    public function boot()
    {
        //

        parent::boot();
    }

    /**
     * Define the routes for the application.
     *
     * @return void
     */
    public function map()
    {
        $this->mapApiRoutes();

        $this->mapWebRoutes();

        //
    }

    /**
     * Define the "web" routes for the application.
     *
     * These routes all receive session state, CSRF protection, etc.
     *
     * @return void
     */
    protected function mapWebRoutes()
    {
        Route::middleware('web')
             ->namespace($this->namespace)
             ->group(base_path('routes/web.php'));
    }

    /**
     * Define the "api" routes for the application.
     *
     * These routes are typically stateless.
     *
     * @return void
     */
    protected function mapApiRoutes()
    {
        Route::prefix('api')
             ->middleware('api')
             ->namespace($this->namespace)
             ->group(base_path('routes/api.php'));
    }
}

Also, when I use this code in routes/web.php it works:

Route::get('/hi', function (){
  return "hi";
});

It's clean Laravel 5.6 installation, on Windows, wamp64.

I tried also with "composer dump auto-load" and "php artisan config:clear" but nothing works.

Thank you in advance.

2个回答



运行以下内容(假设您的应用名称为 app </ strong>):</ p>

< pre> php artisan app:name app
</ code> </ pre>

然后在控制器中也使用以下命名空间:</ p>

 <  code> app \ Http \ Controllers \ Controller 
</ code> </ pre>
</ div>

展开原文

原文

Run the following (assuming your app name is app):

php artisan app:name app

Then use the following namespace also in your controller:

app\Http\Controllers\Controller



问:为什么XXXX如果是HiController? 关于这个问题,如果它返回hi,从技术上讲,它必须工作,如果它是香草。

因为它在Windows和wamp64上,我不认为文件权限是一个东西所以让我们跳过它。</ p>


  1. 确保文件的名称确实正确< / li>
  2. 确保文件确实存在于正确的目录中</ li>
  3. 确保namespce为 namespace App \ Http \ Controllers; </ code> </ li>

  4. 添加此用法,以防万一:使用 App \ Http \ Controllers \ Controller; </ code> </ li>
  5. 为了确保,请确保在您的 composer.json </ code>你有这个(你应该有,因为它是默认的):</ p>

     “autoload”:{
    “psr-4” :{
    “App \”:“app /”
    }
    }
    </ code> </ pre> </ li>
  6. 如果无效,请尝试创建新的 controller php artisan make:controller SomeController </ code>并尝试使用这个。 </ p> </ li>
    </ ol>
    </ div>

展开原文

原文

Q: why XXXX if it's HiController? About the issue, if it returns the hi, technically, it has to work if it's vanilla. Since its on windows and wamp64, I don't think file permissions are a thing so let's skip that.

  1. Make sure the name of the file is indeed correct
  2. Make sure the file indeed exists in the correct directory
  3. Make sure the namespce is namespace App\Http\Controllers;
  4. Add this use, just in case: use App\Http\Controllers\Controller;
  5. Just to be sure, make sure in your composer.json you've got this (you should have since it's default):

    "autoload": {
        "psr-4": {
            "App\\": "app/"
        }
    }
    
  6. If none work, try creating a new controller php artisan make:controller SomeController and try it with this one.

doupin2013
doupin2013 我害怕不,它不是。 这甚至不是laravel的默认文件。 Laravels默认文件位于vendor / laravel / framework / src ....你已经错误地安装了Laravel。 最有可能的是,您似乎已在另一个laravel项目的文件系统中安装了laravel应用程序
2 年多之前 回复
douyinyi7766
douyinyi7766 谢谢@abr的详细解答。 我把HiController.php放在vendor / laravel / laravel / app / Http / Controllers中之后就开始了。 是文件定位的正确方法吗?
2 年多之前 回复
dongzongpeng6474
dongzongpeng6474 是的,它使用<?php。 这可能与.htaccess有某种关联吗?
2 年多之前 回复
dongyi1429
dongyi1429 你确定课程以.php和里面结尾,以<?开头 php并结束?>?
2 年多之前 回复
dpwdldgn43486
dpwdldgn43486 我忘了说我把XXXX而不是HiController,因为我做的每个控制器都抛出同样的错误。 甚至尝试使用SomeController。 此外,当我在cmd中运行route:list时,我得到:“Class App \ Http \ Controllers \ HiController不存在”
2 年多之前 回复
Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问