"Blade로 Chirper 구축 - Chirps 생성"의 두 판 사이의 차이

32번째 줄: 32번째 줄:


==라우팅==
==라우팅==
컨트롤러를 위해 URL을 생성해야 합니다. 이는 프로젝트의 <code>routes</code> 디렉토리에서 관리할 수 있습니다. 리소스 컨트롤러를 사용하기 때문에 단일 <code>Route::resource()</code> 문으로 모든 라우트를 정의하여 기존 URL 구조를 따를 수 있습니다.
우선, 두 개의 라우트를 활성화할 것입니다:
* <code>index</code> 라우트는 폼과 Chirps 목록을 표시합니다.
* <code>store</code> 라우트는 새로운 Chirps를 저장하는 데 사용됩니다.
이 라우트들을 두 개의 미들웨어 뒤에 배치할 것입니다:
* <code>auth</code> 미들웨어는 로그인한 사용자만 라우트에 접근할 수 있도록 합니다.
* <code>verified</code> 미들웨어는 이메일 인증을 활성화할 경우 사용됩니다.
{{소스헤더|routes/web.php}}
<syntaxhighlight lang='php' highlight='3,21-23'>
<?php
use App\Http\Controllers\ChirpController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
Route::get('/', function () {
    return view('welcome');
});
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
Route::resource('chirps', ChirpController::class)
    ->only(['index', 'store'])
    ->middleware(['auth', 'verified']);
require __DIR__.'/auth.php';
</syntaxhighlight>
이 코드는 다음과 같은 라우트를 생성합니다:
{| class='wikitable'
|-
! Verb !! URI !! Action !! Route Name
|-
| GET || <code>/chirps</code> || index || chirps.index
|-
| POST || <code>/chirps</code> || store || chirps.store
|}
{{NOTE}}
모든 라우트를 보려면 <code>php artisan route:list</code> 명령어를 실행하면 됩니다.
{{/NOTE}}
새 <code>ChirpController</code> 클래스의 <code>index</code> 메소드에서 테스트 메시지를 반환하여 라우트와 컨트롤러를 테스트해봅시다:
{{소스헤더|app/Http/Controllers/ChirpController.php}}
<syntaxhighlight lang='php' highlight='3,10-13'>
<?php
...
use Illuminate\Http\Response;
class ChirpController extends Controller
{
    /**
    * Display a listing of the resource.
    */
    public function index(): Response
    {
        return response('Hello, World!');
    }
...
}
</syntaxhighlight>
이전에 로그인된 상태라면 http://localhost:8000/chirps , 또는 Sail을 사용 중이라면 http://localhost/chirps 로 이동하여 메시지를 확인할 수 있습니다.
==Blade==
==Blade==
==네비게이션 메뉴==
==네비게이션 메뉴==

2024년 6월 17일 (월) 23:03 판

1 개요

03. Creating Chirps
03. Chirps 생성

https://bootcamp.laravel.com/blade/creating-chirps

이제 새로운 애플리케이션을 만들 준비가 되었습니다! 사용자들이 Chirps라는 짧은 메시지를 게시할 수 있도록 해봅시다.

2 모델, 마이그레이션, 컨트롤러

사용자들이 'Chirps'를 게시할 수 있도록 하려면 모델, 마이그레이션, 컨트롤러를 만들어야 합니다. 각 개념을 좀 더 깊이 살펴보겠습니다:

  • 모델은 데이터베이스의 테이블과 상호작용할 수 있는 강력하고 즐거운 인터페이스를 제공합니다.
  • 마이그레이션은 데이터베이스의 테이블을 쉽게 생성하고 수정할 수 있게 해줍니다. 이를 통해 애플리케이션이 실행되는 모든 곳에서 동일한 데이터베이스 구조가 유지되도록 보장합니다.
  • 컨트롤러는 애플리케이션에 대한 요청을 처리하고 응답을 반환하는 역할을 합니다.

거의 모든 기능은 이 세 가지 요소가 조화롭게 작동하는 것을 포함하며, artisan make:model 명령어를 사용하면 이 모든 것을 한 번에 생성할 수 있습니다.

Chirp를 위한 모델, 마이그레이션, 리소스 컨트롤러를 생성하려면 다음 명령어를 사용하세요:

php artisan make:model -mrc Chirp

Note

php artisan make:model --help 명령어를 실행하여 사용가능한 모든 옵션을 볼 수 있습니다.

이 명령어는 다음 세 가지 파일을 생성합니다:

  • app/Models/Chirp.php: Eloquent 모델.
  • database/migrations/<timestamp>_create_chirps_table.php: 데이터베이스 테이블을 생성할 데이터베이스 마이그레이션.
  • app/Http/Controllers/ChirpController.php: 들어오는 요청을 처리하고 응답을 반환할 HTTP 컨트롤러.

3 라우팅

컨트롤러를 위해 URL을 생성해야 합니다. 이는 프로젝트의 routes 디렉토리에서 관리할 수 있습니다. 리소스 컨트롤러를 사용하기 때문에 단일 Route::resource() 문으로 모든 라우트를 정의하여 기존 URL 구조를 따를 수 있습니다.

우선, 두 개의 라우트를 활성화할 것입니다:

  • index 라우트는 폼과 Chirps 목록을 표시합니다.
  • store 라우트는 새로운 Chirps를 저장하는 데 사용됩니다.

이 라우트들을 두 개의 미들웨어 뒤에 배치할 것입니다:

  • auth 미들웨어는 로그인한 사용자만 라우트에 접근할 수 있도록 합니다.
  • verified 미들웨어는 이메일 인증을 활성화할 경우 사용됩니다.
routes/web.php
<?php
 
use App\Http\Controllers\ChirpController;
use App\Http\Controllers\ProfileController;
use Illuminate\Support\Facades\Route;
 
Route::get('/', function () {
    return view('welcome');
});
 
Route::get('/dashboard', function () {
    return view('dashboard');
})->middleware(['auth', 'verified'])->name('dashboard');
 
Route::middleware('auth')->group(function () {
    Route::get('/profile', [ProfileController::class, 'edit'])->name('profile.edit');
    Route::patch('/profile', [ProfileController::class, 'update'])->name('profile.update');
    Route::delete('/profile', [ProfileController::class, 'destroy'])->name('profile.destroy');
});
 
Route::resource('chirps', ChirpController::class)
    ->only(['index', 'store'])
    ->middleware(['auth', 'verified']);
 
require __DIR__.'/auth.php';

이 코드는 다음과 같은 라우트를 생성합니다:

Verb URI Action Route Name
GET /chirps index chirps.index
POST /chirps store chirps.store

Note

모든 라우트를 보려면 php artisan route:list 명령어를 실행하면 됩니다.

ChirpController 클래스의 index 메소드에서 테스트 메시지를 반환하여 라우트와 컨트롤러를 테스트해봅시다:

app/Http/Controllers/ChirpController.php
<?php
...
use Illuminate\Http\Response; 
 
class ChirpController extends Controller
{
    /**
     * Display a listing of the resource.
     */
    public function index(): Response 
    {
        return response('Hello, World!');
    }
 ...
}

이전에 로그인된 상태라면 http://localhost:8000/chirps , 또는 Sail을 사용 중이라면 http://localhost/chirps 로 이동하여 메시지를 확인할 수 있습니다.

4 Blade

5 네비게이션 메뉴

6 Chrip 저장하기

7 관계 생성하기

8 대량 할당 보호

9 마이그레이션 업데이트하기

10 테스트하기

10.1 Artisan Tinker

문서 댓글 ({{ doc_comments.length }})
{{ comment.name }} {{ comment.created | snstime }}