라라벨 | redirect(), return view() 글로벌 헴퍼 함수

|

요약

  • redirect() : 글로벌 Helper 함수입니다. 리다이렉트 응답을 만듭니다.
  • Redirect 퍼사드를 사용하는 방법도 있습니다.

두가지 모두 Illuminate\Http\RedirectResponse 인스턴스를 생성하여 호출합니다.

redirect()

return redirect($to = null, $status = 302, $headers = [], $https = null);
 
return redirect('/home');

return redirect()->to('home');
 
return redirect()->route('route.name');

값 전달

Route::get('redirect', function() {
    return redirect()->route('conferences.show', ['conference' -> 99]);
});

값을 전달할 때는 이렇게 진행합니다

뒤로가기

redirect()->back();

세션 기능을 사용해 사용자가 이전에 방문한 페이지가 어디인지 기억해둡니다. back() 메서드를 사용하면 이전에 방문했던 페이지로 리다이렉트 시킬 수 있습니다.

redirect() -> with()

Route::get('redirect-with-key-value' , function() {
    return redirect('dashboard')
        ->with('error', true);
});

Route::get('redirect-with-key-array' , function() {
    return redirect('dashboard')
        ->with(['error' => true , 'message' -> 'Whoops!']);
});

리다이렉트된 페이지에서 필요한 데이터를 정의합니다.

정리

답글 남기기

이메일 주소는 공개되지 않습니다. 필수 필드는 *로 표시됩니다

클릭하여 복사