如何通过Laravel检查URL是否存在?

rot*_*rcz 3 php url laravel

我确实看过这个答案:

如何通过PHP检查URL是否存在?

但是,我想知道Laravel中是否存在可以检查URL是否存在(而不是404)的方法?

luk*_*ter 6

我假设您要检查是否存在与某个URL匹配的路由.

$routes = Route::getRoutes();
$request = Request::create('the/url/you/want/to/check');
try {
    $routes->match($request);
    // route exists
}
catch (\Symfony\Component\HttpKernel\Exception\NotFoundHttpException $e){
    // route doesn't exist
}
Run Code Online (Sandbox Code Playgroud)