リマインダーコントローラの作成
artisan
authentication
password reminders
悩み事
パスワードリマインダーを実装するためのコードを作成したい
解決方法
php artisan auth:reminders-controller
コマンドが利用できます
$ php artisan auth:reminders-controller
以下のルートのハンドラを含み、app/controllers
ディレクトリにファイルが生成されます
- GET /password/remind - パスワードリマインダービューを表示します
- POST /password/remind - パスワードリマインドのPOSTリクエストをハンドルします
- GET /password/reset/{token} - トークンのためのパスワードリセットビューを表示します
- POST /password/reset - パスワードリセットのリクエストをハンドルします
メソッドを利用し、生成されたファイルはcontrollers/RemindersController.php
にあります
<?php
class RemindersController extends Controller
{
/**
* Display the password reminder view.
*
* @return Response
*/
public function getRemind()
{
return \View::make('password.remind');
}
/**
* パスワードリマインドのPOSTリクエストをハンドルします。
*
* @return Response
*/
public function postRemind()
{
switch (\Password::remind(Input::only('email'))) {
case \Password::INVALID_USER:
return \Redirect::back()->with('error', \Lang::get($reason));
case \Password::REMINDER_SENT:
return \Redirect::back()->with('status', \Lang::get($reason));
}
}
/**
* 与えられたトークンのパスワードリセットビューを表示します。
*
* @param string $token
* @return Response
*/
public function getReset($token = null)
{
if (is_null($token)) {
\App::abort(404);
}
return \View::make('password.reset')->with('token', $token);
}
/**
* パスワードのリセットPOSTリクエストをハンドルします。
*
* @return Response
*/
public function postReset()
{
$credentials = \Input::only([
'email', 'password', 'password_confirmation', 'token'
]);
$response = \Password::reset($credentials, function($user, $password) {
$user->password = \Hash::make($password);
$user->save();
});
switch ($response)
{
case \Password::INVALID_PASSWORD:
case \Password::INVALID_TOKEN:
case \Password::INVALID_USER:
return \Redirect::back()->with('error', \Lang::get($response));
case \Password::PASSWORD_RESET:
return \Redirect::to('/');
}
}
}
上記のコントローラーをルートに設定するため
app/routes.php
ファイルに1行追加する必要があります。
\Route::controller('password', 'RemindersController');
アドバイス
以下の変更が必要になります
php artisan auth:reminders-controller
の利用はリマインダーを使い始めるには丁度良いですが、
おそらくアプリケーションにいくつかの変更をしたくなると思います
名前空間を使用している場合、コマンド実行後にRemindersControllerを別の場所に移動し、
適切に編集し、ルートの編集を行います
RESTfulな実装に変更
何かをハンドリングする方法よりコントローラのルーティングを利用したRESTfulな方法に変更をするとよいでしょう 以下の様に行います
- Change the
getRemind()
method toindex()
. - Change the
postRemind()
method tostore()
. - Change the
getReset()
method toshow()
. - Change the
postReset()
method toupdate()
.
これに応じてビューを更新する必要があります。
また、app/routes.php
ファイルも同じく変更する必要があります
Route::resource('password', 'RemindersController', [
'only' => ['index', 'store', 'show', 'update']
]);
Author:Chuck Heintzelman
Category
- App 29
- Artisan 28
- Auth 36
- Basic Development 4
- Blade 23
- Cache 25
- Config 5
- Configuration 12
- Controller 3
- Cookie 2
- Core Extension 7
- Crypt 6
- DB 4
- Database Configuration 3
- Eloquent 0
- File 26
- Form 30
- Hash 1
- Help 2
- Html 17
- Installation 13
- Lang 6
- Middleware 2
- Paginator 1
- Route 1
- Session 0
- Solution 2
- Service Provider 1
- Testing 2
- Packages by 3rd Parties 0