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.
<?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