前提・実現したいこと
管理者のページを作成しようとしています。
参考にしたサイト リンク内容
参考サイトauth.phpにadmin関連設定を追加する。はできています
ログイン画面で管理者専用画面に移動しようとした時に
発生エラーメッセージ
The POST method is not supported for this route. Supported methods: GET, HEAD.
エラーが発生
###web.php
<?php Auth::routes(); Route::group(['prefix' => 'admin'], function() { Route::get('/', function () { return redirect('/admin/images/list'); }); Route::get('login', 'Admin\LoginController@showLoginForm')->name('admin_login'); Route::post('login', 'Admin\LoginController@login'); }); Route::group(['prefix' => 'admin', 'middleware' => 'auth:admin'], function() { Route::get('images/list', 'Admin\ImagesController@index')->name('item_list'); });
###creat_admins_table.php
<?php use Illuminate\Support\Facades\Schema; use Illuminate\Database\Schema\Blueprint; use Illuminate\Database\Migrations\Migration; class CreateAdminsTable extends Migration { public function up() { Schema::create('admins', function (Blueprint $table) { $table->bigIncrements('id'); $table->string('name'); $table->string('email')->unique(); $table->timestamp('email_verified_at')->nullable(); $table->string('password'); $table->rememberToken(); $table->timestamps(); }); } public function down() { Schema::dropIfExists('admins'); } }
Admin.php
1<?php 2 3namespace App; 4 5use Illuminate\Notifications\Notifiable; 6use Illuminate\Foundation\Auth\User as Authenticatable; 7 8class Admin extends Authenticatable 9{ 10 use Notifiable; 11 12 $fillable = [ 13 'name', 'email', 'password', 14]; 15 16protected $hidden = [ 17 'password', 'remember_token', 18]; 19}
app/Excepsions/Handler.php
<?php namespace App\Exceptions; use Exception; use Illuminate\Auth\AuthenticationException; use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler; class Handler extends ExceptionHandler { //default public function render($request, Exception $exception) { return parent::render($request, $exception); } if ($request->expectsJson()) { return response()->json(['error' => 'Unauthenticated.'], 401); //->json=json形式で返す } if (in_array('admin', $exception->guards(), true)) { return redirect()->guest(route('admin.login')); } return redirect()->guest(route('login')); } }
LoginController.php
<?php namespace App\Http\Controllers\Admin; use Illuminate\Support\Facades\Auth; use Illuminate\Http\Request; use Illuminate\Foundation\Auth\AuthenticatesUsers; use App\Http\Controllers\Controller; use App\Admin; class LoginController extends Controller { use AuthenticatesUsers; protected $redirectTo = '/images/list'; public function showLoginForm() { return view('admin.login'); } protected function guard() { return \Auth::guard('admin'); } public function logout(Request $request) { Auth::guard('admin')->logout(); $this->performLogout($request); return redirect()->route('admin_login'); } }
views/admin/login.blade.hp
<!DOCTYPE HTML> <html lang="ja"> <head> //省略 <meta name="csrf-token" content="{{ csrf_token() }}"> <title></title> <script src="{{ secure_asset('js/app.js') }}" defer></script> <link rel="stylesheet" href="https://87c1ac065f9145e183015d2ea2786408.vfs.cloud9.us-east-2.amazonaws.com/css/layout.css"> </head> <body> <div class="container mt-5 pt-5"> //省略 <div class="card-header"><h4>{{ __('ログイン') }}</h4></div> <div class="card-body"> <form method="post" action="{{ route('admin_login') }}" name="login"> @csrf <div class="form-group row"> <label for="email" class="col-md-4 col-form-label text-md-right">{{ __('Eメールアドレス') }}</label> <div class="col-md-6"> <input id="email" type="email" class="form-control @error('email') is-invalid @enderror" name="email" value="{{ old('email') }}" required autocomplete="email" autofocus> //省略 </form> </div> </div> </div> </body> </html>
views/admin/images/list
@extends('admin.admin_layout') @section('title', '画像一覧') @section('content') <div class="container"> <div class="row"> <h2>画像一覧</h2> </div> <div class="row"> <div class="col-md-4"> <a href="{{ route('item_regi') }}" role="button" class="btn btn-primary">新規作成</a> </div> <div class="col-md-8"> <form action="{{ route('item_list') }}" method="get"> <div class="form-group row"> <label class="col-md-2">商品名</label> <div class="col-md-8"> <input type="text" class="form-control" name="cond_title" value="{{ $cond_title }}"> </div> <div class="col-md-2"> {{ csrf_field() }} <input type="submit" class="btn btn-primary" value="検索"> </div> </div> </form> </div> </div> //省略 <div class="row justify-content-center"> {{ $items->links() }} </div> </div> @endsection
試した事
Laravel公式のリファレンスでuseのバージョンが違っていないか確認しました
回答1件
あなたの回答
tips
プレビュー