Laravel入門トップページ


目次

  1. Composerのダウンロードとインストール
  2. コメント掲示板を作成してみよう
  3. リレーションシップを使いこなそう
  4. ユーザ認証の機能を実現しよう
    1. 概要と準備
    2. メールの設定
    3. 認証機能の実装
    4. 学籍番号の登録とログイン名
    5. 登録メールアドレスの認証
    6. パスワードのリセット
    7. プロフィールの表示
    8. パスワードの変更
  5. マルチ認証の機能を実現しよう
  6. MongoDB に接続しよう
  7. キューを利用しよう
  8. コマンド(コンソール)を利用しよう
  9. 本番環境にデプロイしよう

ユーザ認証の機能を実現しよう

概要と準備

目次に戻る

概要

ここでは,プロジェクトにユーザ認証の機能を追加する.つまり,ユーザの登録やログイン,ログアウト,ユーザごとに違った内容のページを表示させる機能の実現である.

目次に戻る

準備作業

プロジェクトを作成する.すでにあるプロジェクトに機能を追加する場合は以下の作業は不要です.

[GakuinHana@rin06 Documents]$ pwd ⏎
/home/GakuinHana/Documents
[GakuinHana@rin06 Documents]$ cd laravel/ ⏎
[GakuinHana@rin06 laravel]$ ls ⏎
composer.phar
[GakuinHana@rin06 laravel]$ php composer.phar create-project --prefer-dist laravel/laravel laravelUser ⏎
You are running composer with xdebug enabled. This has a major impact on runtime performance. See https://getcomposer.org/xdebug
Installing laravel/laravel (v5.4.30)
  - Installing laravel/laravel (v5.4.30)
    Downloading: 100%

Created project in laravelUser

...(中略)...

Generating autoload files
> Illuminate\Foundation\ComposerScripts::postUpdate
> php artisan optimize
Generating optimized class loader
The compiled services file has been removed.
> php artisan key:generate
Application key [base64:zKbvG7T0j9968O/jC6WtYgP8Uvq2yI4dyNpu8XbH+WM=] set successfully.
[GakuinHana@rin06 laravel]$

Git の初期設定をしているのであれば,Git でコミットしておく.

[GakuinHana@rin06 laravel]$ cd laravelUser ⏎
[GakuinHana@rin06 laravelUser]$ ls ⏎
app        composer.json  database      public     routes      tests
artisan    composer.lock  package.json  readme.md  server.php  vendor
bootstrap  config         phpunit.xml   resources  storage     webpack.mix.js
[GakuinHana@rin06 laravelUser]$ git init ⏎
Initialized empty Git repository in /home/GakuinHana/Documents/laravel/laravelUser/.git/
[GakuinHana@rin06 laravelUser]$ git add . ⏎
[GakuinHana@rin06 laravelUser]$ git commit -m"initial commit" ⏎
[master (root-commit) 3fa9439] initial commit
 81 files changed, 6481 insertions(+)

 ...(中略)...

[GakuinHana@rin06 laravelUser]$ git log ⏎
commit 3fa94396620e7b32f5a6ac6e50444c753973b176
Author: Gakuin Hanako <gakuin.hanako@dummy.kobegakuin.ac.jp>
Date:   Thu Jun 14 13:10:55 2018 +0900

    initial commit
[GakuinHana@rin06 laravelUser]$

目次に戻る

.env に設定を記述

.env ファイルを修正する.DB_CONNECTIONsqliteに変更し,DB_HOST, DB_PORT, DB_DATABASE, DB_USERNAME, DB_PASSWORD の行を削除するか,先頭に # を付けてコメントアウトする.

.env (抜粋)
DB_CONNECTION=sqlite
# DB_HOST=127.0.0.1
# DB_PORT=3306
# DB_DATABASE=homestead
# DB_USERNAME=homestead
# DB_PASSWORD=secret

目次に戻る

タイムゾーンと言語の設定

プロジェクトの設定ファイル config/app.php を編集し,タイムゾーンをAsia/Tokyoに,言語をjaに変更する.

config/app.php (抜粋)
    /*
    |--------------------------------------------------------------------------
    | Application Timezone
    |--------------------------------------------------------------------------
    |
    | Here you may specify the default timezone for your application, which
    | will be used by the PHP date and date-time functions. We have gone
    | ahead and set this to a sensible default for you out of the box.
    |
    */

    'timezone' => 'Asia/Tokyo',

    /*
    |--------------------------------------------------------------------------
    | Application Locale Configuration
    |--------------------------------------------------------------------------
    |
    | The application locale determines the default locale that will be used
    | by the translation service provider. You are free to set this value
    | to any of the locales which will be supported by the application.
    |
    */

    'locale' => 'ja',

目次に戻る

空のデータベースファイルを作成する

Sqlite用に空のデータベースファイル database/database.sqlite を作成する.

[GakuinHana@rin06 laravelUser]$ ls ⏎
app        composer.json  database      public     routes      tests
artisan    composer.lock  package.json  readme.md  server.php  vendor
bootstrap  config         phpunit.xml   resources  storage     webpack.mix.js
[GakuinHana@rin06 laravelUser]$ cd database ⏎
[GakuinHana@rin06 database]$ ls ⏎
factories  migrations  seeds
[GakuinHana@rin06 database]$ touch database.sqlite ⏎
[GakuinHana@rin06 database]$ ls ⏎
database.sqlite  factories  migrations  seeds
[GakuinHana@rin06 database]$ cd .. ⏎
[GakuinHana@rin06 laravelUser]$

目次に戻る