doupu3635 2018-07-24 18:58
浏览 1213

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条回答 默认 最新

  • douzhi3779 2018-07-24 21:44
    关注

    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
    
    评论

报告相同问题?

悬赏问题

  • ¥20 usb设备兼容性问题
  • ¥15 错误(10048): “调用exui内部功能”库命令的参数“参数4”不能接受空数据。怎么解决啊
  • ¥15 安装svn网络有问题怎么办
  • ¥15 Python爬取指定微博话题下的内容,保存为txt
  • ¥15 vue2登录调用后端接口如何实现
  • ¥65 永磁型步进电机PID算法
  • ¥15 sqlite 附加(attach database)加密数据库时,返回26是什么原因呢?
  • ¥88 找成都本地经验丰富懂小程序开发的技术大咖
  • ¥15 如何处理复杂数据表格的除法运算
  • ¥15 如何用stc8h1k08的片子做485数据透传的功能?(关键词-串口)