Loka
Loka

Reputation: 41

How to direct random routing (404) to one view page in laravel?

I want to direct random link to only one view except that I've define

e.g : localhost:8000/abxcdss

It will go to a view or home page.

Upvotes: 0

Views: 227

Answers (1)

W Kristianto
W Kristianto

Reputation: 9313

Solution #1 - Via Exception Handler (404)

app/Exceptions/Handler.php

use Symfony\Component\HttpKernel\Exception\NotFoundHttpException;

//

public function render($request, Exception $exception)
{
    if ($exception instanceof NotFoundHttpException) {
        return redirect('/');
    }

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

Solution #2 - Via Route

routes/web.php

// Last line!

Route::any('{any}', function () {
    return redirect('/');
});

Upvotes: 1

Related Questions