본문 바로가기
IT/PHP

Laravel 8 - CRUD(6)

by Sungjun_ 2021. 1. 7.

회원가입

resources/layouts/app.bladel.php 파일로 가서 회원가입 버튼을 만들어줍니다.

<!doctype html>
<html lang="kr">
<head>
    <meta charset="UTF-8">
    <meta name="viewport"
          content="width=device-width, user-scalable=no, initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0">
    <meta http-equiv="X-UA-Compatible" content="ie=edge">
    <link href="{{ asset('css/app.css') }}" rel="stylesheet">
    <title>@yield('title', 'CRUD')</title>
</head>
<body>
@section('header')
    <header class="w-2/3 mx-auto mt-16 text-right">
        <a href="{{route('boards.index')}}" class="text-xl">게시판</a>
        <a href="{{route('auth.register.index')}}" class="text-xl">회원가입</a>
    </header>
@show

@section('section')
@show
</body>
</html>

 

routes/web.php로 가서 라우트를 설정해줍니다.

Route::get('auth/register', function (){
   return view('auth.register');
}) -> name('auth.register.index');

 

resources/views/auth에 register.blade.php 파일을 만들어줍니다.

@extends('layouts.app')

@section('section')
    <div class="w-2/3 mx-auto mt-16">
        <form action="/auth/register" method="post" class="w-1/2 mx-auto">
            @csrf
            <p>
                <label for="email" class="pr-2">이메일</label>
                <input type="email" id="email" name="email"
                       class="outline-none border border-blue-400 rounded-lg pl-1 w-1/3">
            </p>
            <p class="mt-4">
                <label for="password" class="pr-2">비밀번호</label>
                <input type="password" id="password" name="password"
                       class="outline-none border border-blue-400 rounded-lg pl-1 w-1/3">
            </p>
            <p class="mt-4">
                <label for="password_confirmation" class="pr-2">비밀번호 확인</label>
                <input type="password" id="password_confirmation" name="password_confirmation"
                       class="outline-none border border-blue-400 rounded-lg pl-1 w-1/3">
            </p>
            <p class="mt-4">
                <label for="name" class="pr-2">이름</label>
                <input type="text" id="name" name="name"
                       class="outline-none border border-blue-400 rounded-lg pl-1 w-1/3">
            </p>
            <p class="mt-8">
                <input type="submit" value="가입"
                       class="bg-blue-500 hover:bg-blue-700 px-4 py-1 text-lg text-white rounded-lg outline-none">
                <input type="button" class="bg-red-500 hover:bg-red-700 px-4 py-1 ml-4 text-lg text-white rounded-lg outline-none"
                        onclick="window.location.href = '{{route('home')}}'" value="취소">
            </p>
        </form>
    </div>
@stop

 

php artisan make:controller RegisterController를 입력해 컨트롤러를 만들어줍니다.

<?php

namespace App\Http\Controllers;

use App\Models\User;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Hash;

class RegisterController extends Controller
{
    public function index(){
        return view('auth.register');
    }

    public function store(Request $request){
        $validation = $request -> validate([
            'email' => 'required|email',
            'password' => 'required|confirmed',
            'name' => 'required',
        ]);

        $user = new User();
        $user -> email = $validation['email'];
        $user -> password = Hash::make($validation['password']);
        $user -> name = $validation['name'];
        $user -> save();

        return redirect() -> route('home');
    }
}

 

validate를 이용해 먼저 유효성 검사를 해주고 $validation에 저장합니다.

 

그리고 DB에 저장하는데 비밀번호는 해쉬를 사용했습니다.

마지막으로 회원가입을 하고 메인화면으로 가게했습니다.

 

web.php를 수정합니다.

<?php

use Illuminate\Support\Facades\Route;
use App\Http\Controllers\BoardController;
use App\Http\Controllers\RegisterController;


Route::get('/', function () {
    return view('home');
}) -> name('home');

Route::get('auth/register', [RegisterController::class, 'index']) -> name('auth.register.index');
Route::post('auth/register', [RegisterController::class, 'store']) -> name('auth.register.store');

Route::get('boards', [BoardController::class, 'index']) -> name('boards.index');
Route::get('boards/create', [BoardController::class, 'create']) -> name('boards.create');
Route::post('boards', [BoardController::class, 'store']) -> name('boards.store');
Route::get('boards/{board}', [BoardController::class, 'show']) -> name('boards.show');
Route::get('boards/{board}/edit', [BoardController::class, 'edit']) -> name('boards.edit');
Route::put('boards/{board}', [BoardController::class, 'update']) -> name('boards.update');

 

이제 회원가입을 하면 DB에 저장된 것을 확인할 수 있습니다.

회원가입

 

'IT > PHP' 카테고리의 다른 글

Laravel 8 - CURD(8)  (0) 2021.01.08
Laravel 8 - CRUD(7)  (0) 2021.01.07
Laravel 8 - CRUD(5)  (1) 2021.01.05
Laravel 8 - CRUD(4)  (0) 2021.01.05
Laravel 8 - CRUD(3)  (0) 2021.01.05

댓글