I did look at this answer:
How can I check if a URL exists via PHP?
However, I was wondering if a method exists in Laravel that can check if a URL exists (not 404) or not?
I assume you want to check if a there's a route matching a certain 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
}
Not particular laravel function, but you can make a try on this
function urlExists($url = NULL)
{
if ($url == NULL) return false;
$ch = curl_init($url);
curl_setopt($ch, CURLOPT_TIMEOUT, 5);
curl_setopt($ch, CURLOPT_CONNECTTIMEOUT, 5);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
$data = curl_exec($ch);
$httpcode = curl_getinfo($ch, CURLINFO_HTTP_CODE);
curl_close($ch);
return ($httpcode >= 200 && $httpcode < 300) ? true : false;
}
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With