前提
いいね、編集、削除が可能な投稿機能を作っています。
いいね機能を実装後になぜか投稿一覧(index.blade.php)からログアウトしようとする時だけ以下のエラーメッセージが発生しました。(他のページからはログアウトできます)
何が原因でしょうか?
発生している問題・エラーメッセージ
419 | PAGE EXPIRED
該当のソースコード
index.blade.php
@extends('layouts.posts') @section('title', 'index') <table> @section('post_content') @foreach ($items as $item) <tr><th>{{$item->title}}</th></tr> <tr><td>{{$item->content}}</td></td> <tr> <td> {{$item->user->name}} @if ($item->user_id === Auth::user()->id) <form action="posts/{{$item->id}}/edit" method="get"> @csrf <input type="hidden" name="id"> <button>編集する</button> </form> <form action="posts/del/{{$item->id}}" method="post"> @method('delete') @csrf <input type="hidden" name="id"> <button>削除する</button> </form> @endif </td> <tr> <td> @if($item->is_liked_by_auth_user()) <a href="{{ route('unlike_post', $item->id) }}" class="fas fa-heart"> @csrf <span class="badge">{{$item->likes->count()}}</span> </a> @else <a href="{{ route('like_post', $item->id) }}" class="far fa-heart"> @csrf <span class="badge">{{$item->likes->count()}}</span> </a> @endif </td> </tr> @endforeach </table> @endsection
app.blade.php
<!doctype html> <html lang="{{ str_replace('_', '-', app()->getLocale()) }}"> <head> <meta charset="utf-8"> <meta name="viewport" content="width=device-width, initial-scale=1"> <!-- CSRF Token --> <meta name="csrf-token" content="{{ csrf_token() }}"> <title>{{ config('app.name', 'Laravel') }}</title> <!-- Scripts --> <script src="{{ asset('js/app.js') }}" defer></script> <!-- Fonts --> <link rel="dns-prefetch" href="//fonts.gstatic.com"> <link href="https://fonts.googleapis.com/css?family=Nunito" rel="stylesheet"> <!-- Styles --> <link href="{{ asset('css/app.css') }}" rel="stylesheet"> </head> <body> <div id="app"> <nav class="navbar navbar-expand-md navbar-light bg-white shadow-sm"> <div class="container"> <a class="navbar-brand" href="{{ url('/') }}"> {{ config('app.name', 'Laravel') }} </a> <button class="navbar-toggler" type="button" data-toggle="collapse" data-target="#navbarSupportedContent" aria-controls="navbarSupportedContent" aria-expanded="false" aria-label="{{ __('Toggle navigation') }}"> <span class="navbar-toggler-icon"></span> </button> <div class="collapse navbar-collapse" id="navbarSupportedContent"> <!-- Left Side Of Navbar --> <ul class="navbar-nav mr-auto"> </ul> <!-- Right Side Of Navbar --> <ul class="navbar-nav ml-auto"> <!-- Authentication Links --> @guest @if (Route::has('login')) <li class="nav-item"> <a class="nav-link" href="{{ route('login') }}">{{ __('Login') }}</a> </li> @endif @if (Route::has('register')) <li class="nav-item"> <a class="nav-link" href="{{ route('register') }}">{{ __('Register') }}</a> </li> @endif @else <li class="nav-item"> <a class="nav-link" href="/posts">みんなの投稿</a> </li> <li> <a class="nav-link" href="posts/create">投稿を作成</a> </li> <li class="nav-item dropdown"> <a id="navbarDropdown" class="nav-link dropdown-toggle" href="#" role="button" data-toggle="dropdown" aria-haspopup="true" aria-expanded="false" v-pre> {{ Auth::user()->name }} </a> <div class="dropdown-menu dropdown-menu-right" aria-labelledby="navbarDropdown"> <a class="dropdown-item" href="{{ route('logout') }}" onclick="event.preventDefault(); document.getElementById('logout-form').submit();"> {{ __('Logout') }} </a> <form id="logout-form" action="{{ route('logout') }}" method="POST" class="d-none"> @csrf </form> </div> </li> @endguest </ul> </div> </div> </nav> <main class="py-4"> @yield('content') </main> </div> </body> </html>
web.php
<?php use Illuminate\Support\Facades\Route; /* |-------------------------------------------------------------------------- | 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! | */ Route::get('/', function () { return view('welcome'); }); Auth::routes(); Route::get('/home', [App\Http\Controllers\HomeController::class, 'index'])->name('home'); Route::get('posts', 'App\Http\Controllers\PostsController@index'); Route::get('posts/create', 'App\Http\Controllers\PostsController@create'); Route::post('posts/store', 'App\Http\Controllers\PostsController@store'); Route::get('posts/{id}/edit', 'App\Http\Controllers\PostsController@edit'); Route::put('posts/{id}/update', 'App\Http\Controllers\PostsController@update'); Route::delete('posts/del/{id}', 'App\Http\Controllers\PostsController@destroy'); Route::get('/posts/like/{id}','App\Http\Controllers\PostsController@switchLike')->name('like_post'); Route::get('/posts//unlike/{id}','App\Http\Controllers\PostsController@switchUnlike')->name('unlike_post');
試したこと
App\Exceptions\Handler.phpに
public function render($request, Throwable $exception) { // TokenMismatchException 例外発生時 if($exception instanceof \Illuminate\Session\TokenMismatchException) { // ログアウトリクエスト時は、強制的にログアウト if($request->is('logout')) { Auth::logout(); } } return parent::render($request, $exception);
を記述しましたが
このページは動作していません
というエラーが出ます。
補足情報(FW/ツールのバージョンなど)
Windows10pro
docker Version:20.10.7
Laravel Framework 8.50.0
laravel/ui使用
コンパイル済み
(他に必要な情報があれば言ってください)