1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

laravelでログインできるサイトを作ってみよう

Posted at

laravel使って何作ろッと言うことで何を作る?
会員制の物かなぁっと思ったので認証ありのページを作ってみましょう。

まずは環境の作成から

# Laravelのプロジェクト作成
composer create-project --prefer-dist laravel/laravel .
# sailで環境構築
composer require laravel/sail --dev
# 各種ソフトインストール
# mysql,redis,memcached入れます
php artisan sail:install
 ┌ Which services would you like to install? ───────────────────┐
 │ mysql                                                        │
 │ redis                                                        │
 │ memcached                                                    │
 └──────────────────────────────────────────────────────────────┘

環境起動

./vendor/bin/sail up -d
# するとDBつながらないよっていうエラーでるので
# マイグレーション実施
./vendor/bin/sail artisan migrate

認証用のライブラリを追加

composer require laravel/fortify
# マイグレーション実施
php artisan migrate

環境はできた。。。はず。

設定周り

  • 次にfortify使うに当たっての設定
  • /Users/motoki/workspace/check-saiapp/Providers/FortifyServiceProvider.php
  • 下記追加
public function boot(): void 
{
           
           
           Fortify::registerView(function () { // ユーザー登録画面の表示処理
                    return view('auth.register');
           });
           Fortify::loginView(function () { // ログイン画面の表示処理
             		  return view('auth.login');
           });
}
  • /Users/motoki/workspace/check-sail/config/fortify.php
  • 下記追加
'redirects' => [ 'logout' => 'login', ],  // ログアウト時に/loginのURLにリダイレクト
  • /Users/motoki/workspace/check-sail/routes/web.php
  • 下記追加
Route::view('/home', 'auth.home')->middleware('auth');

次はページログイン用のページを記載

  • /Users/motoki/workspace/check-sail/resources/views/auth/login.blade.php
  • viewsの中に auth フォルダを作って上記ファイルを設置
@if ($errors->any())
  <div>
    <ul>
      @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
      @endforeach
    </ul>
  </div>
@endif
<p>ログイン画面</p>
<form method="POST" action="{{ route('login') }}">
  @csrf
  <div>
    <label for="email">メールアドレス</label>
    <input id="email" type="email" name="email">
  </div>
  <div>
    <label for="password">パスワード</label>
    <input id="password" type="password" name="password">
  </div>
  <button type="submit">ログイン</button>
  <a href="{{ route('register') }}">ユーザー登録</a>
</form>
  • /Users/motoki/workspace/check-sail/resources/views/auth/register.blade.php
@if ($errors->any())
    <div>
    <ul>
        @foreach ($errors->all() as $error)
        <li>{{ $error }}</li>
        @endforeach
    </ul>
    </div>
@endif
<p>登録画面</p>
<form method="post" action="{{ route('register') }}">
    @csrf
    <div>
        <label for="name">名前</label>
        <input id="name" type="text" name="name">
    </div>
    <div>
        <label for="email">メールアドレス</label>
        <input id="email" type="text" name="email">
    </div>
    <div>
        <label for="password">パスワード</label>
        <input id="password" type="password" name="password">
    </div>
    <div>
        <label for="password_confirmation">パスワード再入力</label>
        <input id="password_confirmation" type="password" name="password_confirmation">
    </div>
    <button type="submit">ユーザー登録</button>
    <a href="{{ route('login') }}">ログイン</a>
</form>
  • /Users/motoki/workspace/check-sail/resources/views/authhome.blade.php
<p>ホーム画面</p>
<form method="POST" action="{{ route('logout') }}">
  @csrf
  <button type="submit">ログアウト</button>
</form>

を設置して

http://localhost/login へアクセス
すればOK

参照:ジーピーオンライン

1
1
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
1
1

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?