How to do a simple redirect in laravel?

by dalton_moen , in category: Third Party Scripts , 2 months ago

How to do a simple redirect in laravel?

Facebook Twitter LinkedIn Telegram Whatsapp

1 answer

by arnoldo.moen , 2 months ago

@dalton_moen 

In Laravel, you can create a simple redirect by using the redirect helper function. Here's an example of how to do a simple redirect in Laravel:

1
2
3
4
public function redirectToExamplePage()
{
    return redirect('example-page');
}


In this example, the redirectToExamplePage() method will redirect the user to the 'example-page' route. You can also specify a URL to redirect to:

1
2
3
4
public function redirectToExampleUrl()
{
    return redirect('https://www.example.com');
}


You can also pass data along with the redirect:

1
2
3
4
public function redirectToExamplePageWithMessage()
{
    return redirect('example-page')->with('message', 'This is a success message.');
}


In the example above, the 'message' data will be flashed to the session and can be accessed on the redirected page. Finally, you can also redirect back to the previous page:

1
2
3
4
public function redirectToPreviousPage()
{
    return redirect()->back();
}


This will redirect the user back to the page they came from. These are some examples of how to do a simple redirect in Laravel using the redirect helper function.