LoginSignup
0
0

More than 1 year has passed since last update.

Laravelのインストール後にすべきたった一つのこと(IE非対応ページの作成)

Posted at

はじめに

あなたはIEが好きですか?
私は嫌いです。ということで滅ぼしましょう。

※IEでアクセスしたらエラーページに飛ばすって話です

手順

NonSupportedBrowsers.php を新規作成して下記コードをコピペします。

app/Http/Middleware/NonSupportedBrowsers.php
<?php

declare(strict_types=1);

namespace App\Http\Middleware;

use Closure;
use Illuminate\Support\Str;

class NonSupportedBrowsers
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     *
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        if (Str::contains($request->userAgent(), ['Trident', 'MSIE'])) {
            return response()->view('errors.non_supported');
        }

        return $next($request);
    }
}

Kernel.php$middlewareGroups に1行追加します。

app/Http/Kernel.php
protected $middlewareGroups = [
    'web' => [
        \App\Http\Middleware\NonSupportedBrowsers::class, // この行を追加

IEでアクセスしたときに表示するページを作成します。

resources/view/errors/non_supported.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">
  <title>IEは死にました</title>
</head>
<body>
<div>
  <h1>OOPS!</h1>
  <div>あなたが利用しているブラウザ(Internet Explorer)はサポート対象外です。</div>
</div>
</body>
</html>

結果

IEでアクセスするとこうなります。
IE.png

めでたしめでたし❗️✊😸

0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0