Check If Route Exists in Laravel 9

Check If Route Exists in Laravel 9

When implementing a web application, we may need to check whether a route exists. This tutorial provides example how to do it in Laravel 9 application.

We can use the has method of Route facade to determine if a route exists by given name.

app/Http/Controllers/TestController.php

<?php

namespace App\Http\Controllers;

use Illuminate\Http\Response;
use Illuminate\Support\Facades\Route;

class TestController extends Controller
{
    public function index(): Response
    {
        $message = Route::has('wrong') ? 'Route exists'  : 'Route not exists';

        return new Response($message);
    }
}

Leave a Comment

Cancel reply

Your email address will not be published.