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

回答編集履歴

3

修正

2016/11/30 02:05

投稿

退会済みユーザー
answer CHANGED
@@ -131,6 +131,7 @@
131
131
  public function toMail($notifiable)
132
132
  {
133
133
  return (new MailMessage)
134
+ ->subject('パスワードリセット') // メールの件名
134
135
  ->view('admin.emails.password') // resources/views/admin/emails/password.blade.phpを表示します
135
136
  ->action('Reset Password', url('admin/password/reset', $this->token)); // $actionUrlという変数にトークン情報を含めたパスワードリセットのURLを格納します。これは上記のviewで参照可能です。
136
137
  }
@@ -148,4 +149,65 @@
148
149
  $actionUrlは、次のような内容を表示します。http://example.com/admin/password/reset/32c22536d66d7a994c9430a02a61e4498e86949a08ff39116882570d5385f91c
149
150
  <br />
150
151
  <a href="{{ $actionUrl }}">{{ $actionUrl }}</a>
152
+ ```
153
+
154
+ ### 5. リセット後のリダイレクト先の設定
155
+
156
+ リセット完了後のリダイレクト先が通常は /home に飛ぶことになっていますが、これを /admin に飛ぶようにします。
157
+
158
+ app/Http/Controller/Admin/Auth/ResetPasswordController.php
159
+
160
+ ```
161
+ <?php
162
+
163
+ namespace App\Http\Controllers\Admin\Auth;
164
+
165
+ use App\Http\Controllers\Controller;
166
+ use Illuminate\Foundation\Auth\ResetsPasswords;
167
+
168
+ use Illuminate\Http\Request;
169
+
170
+ class ResetPasswordController extends Controller
171
+ {
172
+ /*
173
+ |--------------------------------------------------------------------------
174
+ | Password Reset Controller
175
+ |--------------------------------------------------------------------------
176
+ |
177
+ | This controller is responsible for handling password reset requests
178
+ | and uses a simple trait to include this behavior. You're free to
179
+ | explore this trait and override any methods you wish to tweak.
180
+ |
181
+ */
182
+
183
+ use ResetsPasswords;
184
+
185
+ protected $redirectTo = '/admin'; // 追加
186
+
187
+ /**
188
+ * Display the password reset view for the given token.
189
+ *
190
+ * If no token is present, display the link request form.
191
+ *
192
+ * @param \Illuminate\Http\Request $request
193
+ * @param string|null $token
194
+ * @return \Illuminate\Contracts\View\Factory|\Illuminate\View\View
195
+ */
196
+ public function showResetForm(Request $request, $token = null)
197
+ {
198
+ return view('admin.passwords.reset')->with(
199
+ ['token' => $token, 'email' => $request->email]
200
+ );
201
+ }
202
+
203
+ /**
204
+ * Create a new controller instance.
205
+ *
206
+ * @return void
207
+ */
208
+ public function __construct()
209
+ {
210
+ $this->middleware('guest');
211
+ }
212
+ }
151
213
  ```

2

修正

2016/11/30 02:05

投稿

退会済みユーザー
answer CHANGED
@@ -1,20 +1,34 @@
1
+ ### 2016/11/30 修正
2
+
3
+ 前回提示した内容で確かにviewは表示されるのですが、トークン情報を持ってこれないので、
4
+ TokenMismatchでパスワードのリセットができないという致命的な欠陥を発見しました。
5
+ そこで、トークン情報を引き継ぎ、任意のViewをメール本文に表示する方法がわかったので内容を一部変更します。
6
+
1
7
  fagai様
2
8
  返信ありがとうございます。回答が遅くなり申し訳ありません。
3
9
  configからサクッとする方法はどうもないようですね。やっぱりオーバーライドするしかないようです。
4
10
  次のようにすることで、期待通りの結果を得ることが出来ました。ありがとうございました。
5
11
 
6
- こちらを参考にしました。というかまんまです。
12
+ (修正前)こちらを参考にしました。というかまんまです。
7
13
 
8
- http://stackoverflow.com/questions/39327954/laravel-5-3-redefine-reset-email-blade-template
14
+ [http://stackoverflow.com/questions/39327954/laravel-5-3-redefine-reset-email-blade-template](http://stackoverflow.com/questions/39327954/laravel-5-3-redefine-reset-email-blade-template)
9
15
 
16
+ (修正後)こちらが大変参考になりました。
17
+
18
+ [http://takayukii.me/post/20160914887](http://takayukii.me/post/20160914887)
19
+
10
20
  ### 1. Notificationファイルを作成
11
21
 
22
+ 修正前のものから変更はありません
23
+
12
24
  ```
13
25
  php artisan make:notification CustomPasswordReset
14
26
  ```
15
27
 
16
28
  ### 2. User.phpを修正
17
29
 
30
+ 修正前のものから変更はありません
31
+
18
32
  app/User.php
19
33
 
20
34
  ```php
@@ -59,14 +73,10 @@
59
73
  }
60
74
  ```
61
75
 
62
- ### 3. テンプレーファイルを生成
76
+ ### 3. リセッメールを再定義
63
77
 
64
- ```
65
- php artisan vendor:publish --tag=laravel-notifications
78
+ 修正前のものから内容を全て書き換えています。
66
- ```
67
79
 
68
- ### 4. リセットメールを再定義
69
-
70
80
  app/Notifications/CustomPasswordReset.php
71
81
 
72
82
  ```php
@@ -79,25 +89,33 @@
79
89
  use Illuminate\Contracts\Queue\ShouldQueue;
80
90
  use Illuminate\Notifications\Messages\MailMessage;
81
91
 
92
+ use Illuminate\Auth\Notifications\ResetPassword; // 追加
93
+
82
- class CustomPasswordReset extends Notification
94
+ class CustomPasswordReset extends ResetPassword // extendsをNotificationからResetPasswordに変更
83
95
  {
96
+ /**
97
+ * The password reset token.
98
+ *
99
+ * @var string
100
+ */
84
- use Queueable;
101
+ public $token;
85
102
 
86
103
  /**
87
- * Create a new notification instance.
104
+ * Create a notification instance.
88
105
  *
106
+ * @param string $token
89
107
  * @return void
90
108
  */
91
- public function __construct()
109
+ public function __construct($token)
92
110
  {
93
- //
111
+ $this->token = $token;
94
112
  }
95
113
 
96
114
  /**
97
- * Get the notification's delivery channels.
115
+ * Get the notification's channels.
98
116
  *
99
117
  * @param mixed $notifiable
100
- * @return array
118
+ * @return array|string
101
119
  */
102
120
  public function via($notifiable)
103
121
  {
@@ -105,27 +123,29 @@
105
123
  }
106
124
 
107
125
  /**
108
- * Get the mail representation of the notification.
126
+ * Build the mail representation of the notification.
109
127
  *
110
128
  * @param mixed $notifiable
111
129
  * @return \Illuminate\Notifications\Messages\MailMessage
112
130
  */
113
131
  public function toMail($notifiable)
114
132
  {
133
+ return (new MailMessage)
115
- return (new MailMessage)->view('admin.emails.password'); // ここ変更
134
+ ->view('admin.emails.password') // resources/views/admin/emails/password.blade.php表示します
135
+ ->action('Reset Password', url('admin/password/reset', $this->token)); // $actionUrlという変数にトークン情報を含めたパスワードリセットのURLを格納します。これは上記のviewで参照可能です。
116
136
  }
137
+ }
138
+ ```
117
139
 
118
- /**
140
+ ### 4. リセットメールのViewを作成
141
+
142
+ ```
143
+ touch resources/views/admin/emails/password.blade.php
144
+ ```
145
+
146
+ ```
119
- * Get the array representation of the notification.
147
+ パスワードを再設定します的な文章を書きます。Doctype宣言やhtmlタグやbodyタグは不要です。
120
- *
121
- * @param mixed $notifiable
122
- * @return array
123
- */
124
- public function toArray($notifiable)
148
+ $actionUrlは、次のような内容を表示します。http://example.com/admin/password/reset/32c22536d66d7a994c9430a02a61e4498e86949a08ff39116882570d5385f91c
125
- {
126
- return [
127
- //
149
+ <br />
128
- ];
150
+ <a href="{{ $actionUrl }}">{{ $actionUrl }}</a>
129
- }
130
- }
131
151
  ```

1

修正

2016/11/30 01:50

投稿

退会済みユーザー
answer CHANGED
@@ -15,8 +15,10 @@
15
15
 
16
16
  ### 2. User.phpを修正
17
17
 
18
- ```php:app/User.php
18
+ app/User.php
19
19
 
20
+ ```php
21
+
20
22
  <?php
21
23
 
22
24
  namespace App;
@@ -65,7 +67,9 @@
65
67
 
66
68
  ### 4. リセットメールを再定義
67
69
 
68
- ```php:app/Notifications/CustomPasswordReset.php
70
+ app/Notifications/CustomPasswordReset.php
71
+
72
+ ```php
69
73
  <?php
70
74
 
71
75
  namespace App\Notifications;