【Laravel6.0】ログイン機能を実装(ついでに日本語化)
- 2020.01.01
- Laravel
- Laravel, Laravel6.0, ログイン機能, 日本語化
一から自分でログイン機能を実装するのは、セキュリティ面も考慮すると相当大変ですが、Laravelならセキュリティ面も含めて簡単に実装できるんですね。(Laravelすげ~)
目次
ログイン機能を実装
前提
・Laravel6.0がインストールされている
・node.jsがインストールされている
・データベースの準備ができている
・マイグレーションが済んでいる(これは後からでもOK)
実装
# Laravel/uiパッケージのインストール $ composer require laravel/ui:1.x # 実装 $ php artisan ui vue --auth # npmパッケージのインストール $ npm install # ビルド $ npm run dev
以上、4つのコマンドで、実装は完了です。
え…簡単すぎる。
日本語化
出来上がったログインフォームやエラーメッセージなどは、すべて英語で表示されるので、これを日本語に置き換え。
といっても簡単で、1つのファイルの書き換えと、5つのファイルの追加で、簡単に日本語化する事ができます。
Laravelの設定を日本語に
「config」というフォルダの中の、「app.php」というファイルの一部を書き換える。
'locale' => 'en', という行を見つけて、 'locale' => 'ja', に変更する
「’fallback_locale’ => ‘en’, 」という行は、「’ja’」に変更しない方がいいらしい。
日本語(翻訳)ファイルを追加
「resources」の中の「lang」というフォルダの中に、以下のファイルを追加する。
・ja.json
「resources」の中の「lang」 というフォルダの中に「ja」というフォルダを作り、以下の4つのファイルを追加する。
・auth.php
・pagination.php
・passwords.php
・validation.php
それぞれのファイルの中身は以下の通り。(そのままコピペでOK)
ja.json
{
"Login": "ログイン",
"Register": "新規登録",
"Forgot Your Password?": "パスワードを忘れた場合",
"Reset Password": "パスワード再設定",
"Send Password Reset Link": "パスワード再設定URLを送信",
"Name": "ユーザー名",
"E-Mail Address": "メールアドレス",
"Password": "パスワード",
"Confirm Password": "パスワード(確認用)",
"Remember Me": "ログイン状態を保存"
}
auth.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Authentication Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used during authentication for various
| messages that we need to display to the user. You are free to modify
| these language lines according to your application's requirements.
|
*/
'failed' => 'ログインできません。入力した情報に誤りがないかご確認ください。',
'throttle' => 'ログイン試行回数を超えました。:seconds秒後に再度お試しください。',
];
pagination.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Pagination Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are used by the paginator library to build
| the simple pagination links. You are free to change them to anything
| you want to customize your views to better match your application.
|
*/
'previous' => '« 前へ',
'next' => '次へ »',
];
passwords.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Password Reset Language Lines
|--------------------------------------------------------------------------
|
| The following language lines are the default lines which match reasons
| that are given by the password broker for a password update attempt
| has failed, such as for an invalid token or invalid new password.
|
*/
'password' => 'パスワードは6文字以上にして、確認用入力欄と一致させてください。',
'reset' => 'パスワードをリセットしました。',
'sent' => 'パスワードリセット用URLを送信しました。',
'token' => 'パスワードリセット用トークンが不正です。',
'user' => 'メールアドレスに一致するユーザーが見つかりません。',
];
validation.php
<?php
return [
/*
|--------------------------------------------------------------------------
| Validation Language Lines
|--------------------------------------------------------------------------
|
| The following language lines contain the default error messages used by
| the validator class. Some of these rules have multiple versions such
| as the size rules. Feel free to tweak each of these messages here.
|
*/
'accepted' => ':attributeを承認してください。',
'active_url' => ':attributeに正しいURLを入力してください。',
'after' => ':attributeは:dateより先の日付を入力してください。',
'after_or_equal' => ':attributeは:date以降の日付を入力してください。',
'alpha' => ':attributeは英字で入力してください。',
'alpha_dash' => ':attributeは英数字とハイフン、アンダーバーのみで入力してください。',
'alpha_num' => ':attributeは英数字で入力してください。',
'array' => ':attributeは配列で入力してください。',
'before' => ':attributeは:dateより前の日付を入力してください。',
'before_or_equal' => ':attributeは:date以前の日付を入力してください。',
'between' => [
'numeric' => ':attributeは:min〜:maxの範囲で入力してください。',
'file' => ':attributeは:min〜:max KBのファイルを選択してください。',
'string' => ':attributeは:min〜:max文字の範囲で入力してください。',
'array' => ':attributeは:min〜:max個の範囲内にしてください。',
],
'boolean' => ':attributeはtrueかfalseにしてください。',
'confirmed' => ':attributeが確認用と一致しません。',
'date' => ':attributeを正しい日付で入力してください。',
'date_equals' => ':attributeを:dateと一致するよう入力してください。',
'date_format' => ':attributeの書式を:formatに沿って入力してください。',
'different' => ':attributeと:otherは違うものを入力してください。',
'digits' => ':attributeは:digits桁で入力してください。',
'digits_between' => ':attributeは:min〜:max桁で入力してください。',
'dimensions' => ':attributeの画像サイズが不正です。',
'distinct' => ':attributeが重複しています。',
'email' => ':attributeを正しい形式で入力してください。',
'ends_with' => ':attributeを:valuesで終わるよう入力してください。',
'exists' => '選択した値が不正です。',
'file' => ':attributeはファイルを選択してください。',
'filled' => ':attributeを入力してください。',
'gt' => [
'numeric' => ':attributeは:valueより多く入力してください。',
'file' => ':attributeは:value KBより大きいファイルを選択してください。',
'string' => ':attributeは:value文字より多く入力してください。',
'array' => ':attributeは:value個より多くしてください。',
],
'gte' => [
'numeric' => ':attributeは:value以上で入力してください。',
'file' => ':attributeは:value KB以上のファイルを選択してください。',
'string' => ':attributeは:value文字以上入力してください。',
'array' => ':attributeは:value個以上にしてください。',
],
'image' => ':attributeは画像にしてください。',
'in' => ':attributeは不正です。',
'in_array' => ':attributeは:otherの範囲外です。',
'integer' => ':attributeは数字で入力してください。',
'ip' => ':attributeはIPアドレス形式で入力してください。',
'ipv4' => ':attributeはIPv4形式で入力してください。',
'ipv6' => ':attributeはIPv6形式で入力してください。',
'json' => ':attributeはJSON形式で入力してください。',
'lt' => [
'numeric' => ':attributeは:valueより少なく入力してください。',
'file' => ':attributeは:value KBより小さいファイルを選択してください。',
'string' => ':attributeは:value文字より少なく入力してください。',
'array' => ':attributeは:value個より少なくしてください。',
],
'lte' => [
'numeric' => ':attributeは:value以下で入力してください。',
'file' => ':attributeは:value KB以下のファイルを選択してください。',
'string' => ':attributeは:value文字以下入力してください。',
'array' => ':attributeは:value個以下にしてください。',
],
'max' => [
'numeric' => ':attributeは:max以下で入力してください。',
'file' => ':attributeは:max KB以下のファイルを選択してください。',
'string' => ':attributeは:max文字以下入力してください。',
'array' => ':attributeは:max個以下にしてください。',
],
'mimes' => ':attributeは:values形式で選択してください。',
'mimetypes' => ':attributeは:values形式で選択してください。',
'min' => [
'numeric' => ':attributeは:min以上で入力してください。',
'file' => ':attributeは:min KB以上のファイルを選択してください。',
'string' => ':attributeは:min文字以上入力してください。',
'array' => ':attributeは:min個以上にしてください。',
],
'not_in' => ':attributeは不正です。',
'not_regex' => ':attributeの書式が不正です。',
'numeric' => ':attributeは数字で入力してください。',
'present' => ':attributeは存在する必要があります。',
'regex' => ':attributeの書式が不正です。',
'required' => ':attributeを入力してください。',
'required_if' => ':otherが:valueの時、:attributeを入力してください。',
'required_unless' => ':otherが:valuesでない時、:attributeを入力してください。',
'required_with' => ':valuesが存在する時、:attributeを入力してください。',
'required_with_all' => ':valuesが存在する時、:attributeを入力してください。',
'required_without' => ':valuesが存在しない時、:attributeを入力してください。',
'required_without_all' => ':valuesが存在しない時、:attributeを入力してください。',
'same' => ':attributeと:otherが一致するよう入力してください。',
'size' => [
'numeric' => ':attributeは:sizeで入力してください。',
'file' => ':attributeは:size KBのファイルを選択してください。',
'string' => ':attributeは:size文字で入力してください。',
'array' => ':attributeは:size個にしてください。',
],
'starts_with' => ':attributeを:valuesから始まるよう入力してください。',
'string' => ':attributeは文字列で入力してください。',
'timezone' => ':attributeを正しいタイムゾーンで入力してください。',
'unique' => ':attributeは既に取得されているため、違うものを入力してください。',
'uploaded' => ':attributeはアップロードに失敗しました。',
'url' => ':attributeを正しいURLで入力してください。',
'uuid' => ':attributeを正しいUUIDで入力してください。',
/*
|--------------------------------------------------------------------------
| Custom Validation Language Lines
|--------------------------------------------------------------------------
|
| Here you may specify custom validation messages for attributes using the
| convention "attribute.rule" to name the lines. This makes it quick to
| specify a specific custom language line for a given attribute rule.
|
*/
'custom' => [
'attribute-name' => [
'rule-name' => 'custom-message',
],
],
/*
|--------------------------------------------------------------------------
| Custom Validation Attributes
|--------------------------------------------------------------------------
|
| The following language lines are used to swap our attribute placeholder
| with something more reader friendly such as "E-Mail Address" instead
| of "email". This simply helps us make our message more expressive.
|
*/
'attributes' => [
'email' => 'メールアドレス',
'password' => 'パスワード',
],
];
まとめ
というわけで、とても簡単にログイン機能を実装できました。他にもLaravelにはいろんな便利な機能が用意されているので、少しずつ勉強していきたいと思います。
-
前の記事
Laravel 標準のログアウト先を変更する方法 2019.11.08
-
次の記事
【Laravel】CSRFトークンを付けているのに、status419エラー 2020.01.20
コメントを書く