0

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

Errorの内容

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

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

web.php

<?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');

HomeController.php

<?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]);
    }

}

index.blade.php

@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

HomeMiddleware.php

<?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

1 件の回答 1

1

これを、

// use App\Http\Middleware\HomeMiddleware;

Route::get('home', 'HomeMiddleware@index')->middleware(HomeMiddleware::class);

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

use App\Http\Middleware\HomeMiddleware;

Route::get('home', 'HomeController@index')->middleware(HomeMiddleware::class);

この質問に回答するには、ログインする必要があります。

求めていた回答ではありませんか? のタグが付いた他の質問を参照する。