2015-01-09 105 views

回答

3

我假设你想检查是否有一个匹配某个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 
} 
+0

有没有办法通过Laravel检查外部URL? – rotaercz

+0

@ rotaercz至少不是一个更好的那个,你在你的问题中链接的那个...... – lukasgeiter

+0

啊,好的。谢谢。 – rotaercz

0

试试这个功能

function checkRoute($route) { 
    $routes = \Route::getRoutes()->getRoutes(); 
    foreach($routes as $r){ 
     if($r->getUri() == $route){ 
      return true; 
     } 
    } 

    return false; 
} 
+1

这对路由参数不起作用 - 使用'$ routes-> match()'会解决这个问题。 – pseux

0

不是特别laravel功能,但你可以在这个

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); 
    if ($httpcode >= 200 && $httpcode < 300) { 
     return true; 
    } else { 
     return false; 
}