Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I check if a URL exists via Laravel?

Tags:

url

php

laravel

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?

like image 493
rotaercz Avatar asked Jan 09 '15 05:01

rotaercz


2 Answers

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
}
like image 94
lukasgeiter Avatar answered Oct 04 '22 19:10

lukasgeiter


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;        
   }
like image 25
Bhargav Kaklotara Avatar answered Oct 04 '22 20:10

Bhargav Kaklotara