teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

追記

2019/09/26 08:48

投稿

退会済みユーザー
answer CHANGED
@@ -70,6 +70,8 @@
70
70
 
71
71
  `app/Http/Kernel.php` へミドルウェアの短縮キーを登録した上で、
72
72
 
73
+ [https://readouble.com/laravel/5.8/ja/middleware.html](https://readouble.com/laravel/5.8/ja/middleware.html)
74
+
73
75
  ```php
74
76
  Route::middleware(['is_smartphone'])->group(function () {
75
77
  // スマホのときに表示するルートを記述

1

追記

2019/09/26 08:48

投稿

退会済みユーザー
answer CHANGED
@@ -1,11 +1,78 @@
1
1
  jenssegers/agent というユーザーエージェントのライブラリがあるので、これを使い、
2
2
  [https://github.com/jenssegers/agent](https://github.com/jenssegers/agent)
3
3
 
4
+ ```bash
4
- middleware で判定部分を実装して、
5
+ composer require jenssegers/agent
6
+ ```
5
7
 
8
+ middleware で判定部分を実装します。
9
+
10
+ ```bash
11
+ php artisan make:middleware CheckSmartPhone
12
+ ```
13
+
14
+ 上記のコマンドで以下のファイルが生成される
15
+
6
16
  ```php
17
+ <?php
18
+
19
+ /**
20
+ * app/Http/Middleware/CheckSmartPhone.php
21
+ */
22
+ namespace App\Http\Middleware;
23
+
24
+ use Closure;
25
+
26
+ class CheckSmartPhone
27
+ {
28
+ /**
29
+ * Handle an incoming request.
30
+ *
31
+ * @param \Illuminate\Http\Request $request
32
+ * @param \Closure $next
33
+ * @return mixed
34
+ */
35
+ public function handle($request, Closure $next)
36
+ {
37
+ return $next($request);
38
+ }
39
+ }
40
+ ```
41
+
42
+ これに、以下のように実装して
43
+
44
+ ```php
45
+ <?php
46
+
47
+ namespace App\Http\Middleware;
48
+
49
+ use Closure;
50
+ use Jenssegers\Agent\Agent;
51
+
52
+ class CheckSmartPhone
53
+ {
54
+ /**
55
+ * Handle an incoming request.
56
+ *
57
+ * @param \Illuminate\Http\Request $request
58
+ * @param \Closure $next
59
+ * @return mixed
60
+ */
61
+ public function handle($request, Closure $next)
62
+ {
63
+ if ((new Agent())->isMobile()) {
64
+ return redirect('non-smart-phone', 403);
65
+ }
66
+ return $next($request);
67
+ }
68
+ }
69
+ ```
70
+
71
+ `app/Http/Kernel.php` へミドルウェアの短縮キーを登録した上で、
72
+
73
+ ```php
7
74
  Route::middleware(['is_smartphone'])->group(function () {
8
- //
75
+ // スマホのときに表示するルートを記述
9
76
  }
10
77
  ```
11
78