"Blade로 Chirper 구축 - Chirps 삭제"의 두 판 사이의 차이

40번째 줄: 40번째 줄:


==컨트롤러 업데이트하기==
==컨트롤러 업데이트하기==
이제 <code>ChirpController</code> 클래스의 <code>destroy</code> 메소드를 업데이트하여 삭제 작업을 수행하고 Chirp 인덱스로 돌아가게 할 수 있습니다:
{{소스헤더|app/Http/Controllers/ChirpController.php}}
<syntaxhighlight lang='php' highlight='9-16'>
<?php
...
class ChirpController extends Controller
{
    ...
    /**
    * Remove the specified resource from storage.
    */
    public function destroy(Chirp $chirp): RedirectResponse
    {
        Gate::authorize('delete', $chirp);
        $chirp->delete();
        return redirect(route('chirps.index'));
    }
}
</syntaxhighlight>
==인가==
==인가==
==뷰 업데이트하기==
==뷰 업데이트하기==

2024년 6월 18일 (화) 22:09 판

1 개요

06. Deleting Chirps
06. Chirps 삭제

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

이번에는 사용자가 자신의 Chirp을 삭제할 수 있는 기능을 제공합시다.

이제 어떻게 하는지 감을 잡기 시작했을 거라고 생각합니다. 이 기능을 얼마나 빠르게 추가할 수 있는지에 대해 여러분이 감명받을 거라고 생각합니다.

2 라우팅

chirps.destroy 라우트를 활성화하기 위해 다시 라우트를 업데이트합니다:

routes/web.php
<?php
...
Route::resource('chirps', ChirpController::class)
    ->only(['index', 'store', 'edit', 'update', 'destroy'])
    ->middleware(['auth', 'verified']);
...

이제 이 컨트롤러의 라우트 테이블은 다음과 같습니다:

Verb URI Action Route Name
GET /chirps index chirps.index
POST /chirps store chirps.store
GET /chirps/{chirp}/edit edit chirps.edit
PUT/PATCH /chirps/{chirp} update chirps.update
DELETE /chirps/{chirp} destroy chirps.destroy

3 컨트롤러 업데이트하기

이제 ChirpController 클래스의 destroy 메소드를 업데이트하여 삭제 작업을 수행하고 Chirp 인덱스로 돌아가게 할 수 있습니다:

app/Http/Controllers/ChirpController.php
<?php
...
class ChirpController extends Controller
{
    ...
    /**
     * Remove the specified resource from storage.
     */
    public function destroy(Chirp $chirp): RedirectResponse
    {
        Gate::authorize('delete', $chirp);
 
        $chirp->delete();
 
        return redirect(route('chirps.index'));
    }
}

4 인가

5 뷰 업데이트하기

6 테스트하기

만약 마음에 들지 않는 Chirp를 올렸다면, 삭제해보세요!

Chirp-delete-blade.png

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