HTTP Redirects
- Creating Redirects
- Redirecting To Named Routes
- Redirecting To Controller Actions
- Redirecting With Flashed Session Data
Creating Redirects
리디렉션 응답은 Illuminate\Http\RedirectResponse 클래스의 인스턴스이며, 사용자를 다른 URL로 이동시키는 데 필요한 적절한 헤더를 포함합니다. RedirectResponse 인스턴스를 생성하는 방법은 여러 가지가 있지만, 가장 간단한 방법은 전역 redirect 헬퍼 함수를 사용하는 것입니다.
Route::get('/dashboard', function () {
return redirect('/home/dashboard');
});
폼 제출이 유효하지 않은 경우처럼, 사용자를 이전 페이지로 되돌리고 싶을 때가 있습니다. 이때는 전역 back 헬퍼 함수를 사용할 수 있습니다. 이 기능은 session을 사용하므로, back 함수를 호출하는 라우트가 반드시 web 미들웨어 그룹을 사용하거나, 모든 세션 관련 미들웨어가 적용되어 있어야 합니다.
Route::post('/user/profile', function () {
// Validate the request...
return back()->withInput();
});
Redirecting To Named Routes
매개변수 없이 redirect 헬퍼를 호출하면 Illuminate\Routing\Redirector 인스턴스가 반환되며, 이를 통해 Redirector의 모든 메서드를 사용할 수 있습니다. 예를 들어, 이름이 지정된 라우트로 RedirectResponse를 생성하려면 route 메서드를 사용하면 됩니다.
return redirect()->route('login');
라우트에 파라미터가 필요한 경우, route 메서드의 두 번째 인수로 파라미터를 전달할 수 있습니다.
// For a route with the following URI: profile/{id}
return redirect()->route('profile', ['id' => 1]);
편의를 위해 Laravel에서는 전역 to_route 함수도 제공합니다.
return to_route('profile', ['id' => 1]);
Populating Parameters Via Eloquent Models
만약 "ID" 파라미터를 요구하는 라우트로 이동할 때, 파라미터 값을 Eloquent 모델에서 가져오고 싶다면, 단순히 모델 인스턴스 자체를 전달하면 됩니다. Laravel이 내부적으로 해당 모델의 ID 값을 자동으로 추출해 사용합니다.
// For a route with the following URI: profile/{id}
return redirect()->route('profile', [$user]);
라우트 파라미터에 들어갈 값을 커스터마이즈하고 싶다면, Eloquent 모델에서 getRouteKey 메서드를 오버라이드하면 됩니다.
/**
* Get the value of the model's route key.
*
* @return mixed
*/
public function getRouteKey()
{
return $this->slug;
}
Redirecting To Controller Actions
controller actions으로 리디렉션을 생성할 수도 있습니다. 이때는 action 메서드에 컨트롤러 클래스와 액션명을 전달하세요.
use App\Http\Controllers\HomeController;
return redirect()->action([HomeController::class, 'index']);
컨트롤러 라우트에 파라미터가 필요한 경우, action 메서드의 두 번째 인수로 파라미터를 전달할 수 있습니다.
return redirect()->action(
[UserController::class, 'profile'], ['id' => 1]
);
Redirecting With Flashed Session Data
리디렉션하면서 flashing data to the session하는 작업은 보통 한 번에 처리됩니다. 예를 들어, 어떤 동작이 성공적으로 수행된 후 성공 메시지를 세션에 저장하고 리디렉션하는 경우가 일반적입니다. 보다 편리하게, RedirectResponse 인스턴스를 생성한 후 플루언트 방식으로 메서드 체이닝을 통해 세션에 데이터를 플래시할 수 있습니다.
Route::post('/user/profile', function () {
// Update the user's profile...
return redirect('/dashboard')->with('status', 'Profile updated!');
});
현재 요청의 입력값을 세션에 플래시하여, 사용자가 새로운 위치로 리디렉션되기 전에 RedirectResponse 인스턴스의 withInput 메서드를 사용할 수도 있습니다. 이 방식으로 입력값이 세션에 플래시되면, 다음 요청에서 쉽게 retrieve it할 수 있습니다.
return back()->withInput();
사용자가 리디렉션된 후, session에 저장된 플래시 메시지를 화면에 출력할 수 있습니다. 예를 들어, Blade syntax을 사용하는 방법은 다음과 같습니다.
@if (session('status'))
<div class="alert alert-success">
{{ session('status') }}
</div>
@endif