A
A
Alexey Bolgov2021-03-04 06:34:24
Laravel
Alexey Bolgov, 2021-03-04 06:34:24

How to make a redirect in laravel if the page does not exist?

Hello. Tell me how to make a redirect to the main page of the site in laravel if the entered link does not exist? Or, on error, it also redirected, for example, to 500.blare.php

Answer the question

In order to leave comments, you need to log in

1 answer(s)
R
Roman Sarvarov, 2021-03-04
@Alex2412729

Two ways:
1. in routes/web.php (to the end)

Route::fallback(function ()
{
    return redirect()->to('/');
});

2. Or in/app/Exceptions/Handler.php
/**
 * Render an exception into a response.
 *
 * @param  \Illuminate\Http\Request  $request
 * @param  \Exception  $e
 * @return \Illuminate\Http\Response
 */
public function render($request, Exception $e)
{
    if ($e instanceof NotFoundHttpException) {
        return redirect()->to('/');
    }

    return parent::render($request, $e);
}

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question