

Laravelの勉強のために書籍のコードを丸写ししたつもりが下記のErrorが出てしまいました。  
どうかご教示頂けると大変ありがたいです。

Errorの内容

[code]

    ReflectionException
    Class App\Http\Controllers\HomeMiddleware does not exist
    
    Class App\Http\Controllers\HomeMiddleware does not exist   /Users/horikawatokiya/Diagnosis/diagnosis/vendor/laravel/framework/src/Illuminate/Container/Container.php#729
        ReflectionException
    
[/code]

関係していると思われるファイル

web.php

[code]

    <?php
    
    /*
    |--------------------------------------------------------------------------
    | Web Routes
    |--------------------------------------------------------------------------
    |
    | Here is where you can register web routes for your application. These
    | routes are loaded by the RouteServiceProvider within a group which
    | contains the "web" middleware group. Now create something great!
    |
    */
    // use App\Http\Middleware\HomeMiddleware;
    
    Route::get('home', 'HomeMiddleware@index')->middleware(HomeMiddleware::class);
    Route::post('home', 'HomeController@post');
    
[/code]

HomeController.php

[code]

    <?php
    
    namespace App\Http\Controllers;
    
    use Illuminate\Http\Request;
    use Illuminate\Http\Response;
    
    class HomeController extends Controller {
    
        public function index(Request $request) {
            echo "test";
            return view('home.index', ['data' => $request->data]);
        }
    
        public function post(Request $request) {
            return view('home.index', ['msg' => $request->msg]);
        }
    
    }
    
[/code]

index.blade.php

[code]

    @extends('layouts.diagnosis')
    
    @section('title', 'Index')
    
    @section('menubar')
        @parent
        インデックスページ
    @endsection
    
    @section('content')
        <p>ここが本文のコンテンツです。</p>
        <table>
        @foreach($data as $item)
        <tr>
            <th>
                {{$item['name']}}
            </th>
            <td>
                {{$item['mail']}}
            </td>
        </tr>
        </table>
    @endsection
    
[/code]

HomeMiddleware.php

[code]

    <?php
    
    namespace App\Http\Middleware;
    
    use Closure;
    
    class HomeMiddleware {
        /**
         * Handle an incoming request.
         *
         * @param  \Illuminate\Http\Request  $request
         * @param  \Closure  $next
         * @param  string|null  $guard
         * @return mixed
         */
        public function handle($request, Closure $next) {
            $data = [
                ['name' => 'taro', 'mail' => 'taro@mail'],
                ['name' => 'hanako', 'mail' => 'hanako@mail'],
                ['name' => 'sachico', 'mail' => 'sachico@mail'],
            ];
            $request->merge(['data' => $data]);
            return $next($request);
        }
    }
    
        @section('footer')
        copyright 2017 tuyano.
        @endsection
    
[/code]


CC BY-SA 4.0
2018-06-09T09:52:55.880
44645
2022-01-09T23:02:25.207
2018-06-09T11:14:14.520
13199
28849
question
php laravel
LaravelでHomeMiddlewareの設定時にReflectionExceptionのErrorが解けません。


これを、

[code]

    // use App\Http\Middleware\HomeMiddleware;
    
    Route::get('home', 'HomeMiddleware@index')->middleware(HomeMiddleware::class);
    
[/code]

↓ これで動くんじゃないかな。

[code]

    use App\Http\Middleware\HomeMiddleware;
    
    Route::get('home', 'HomeController@index')->middleware(HomeMiddleware::class);
    
[/code]


CC BY-SA 4.0
2018-06-29T07:24:35.023
45152
2018-06-29T07:24:35.023
28832
44645
answer