Php laravel:异常处理
我有这个功能,它能够获得网址的状态码,如果提供了一个假网址,它只是通过说“假网址”的异常。我想处理这个异常,并将其视为404,该网站已关闭Php laravel:异常处理,php,laravel,Php,Laravel,我有这个功能,它能够获得网址的状态码,如果提供了一个假网址,它只是通过说“假网址”的异常。我想处理这个异常,并将其视为404,该网站已关闭 private function getStatusCode($url) { $response = $this->client->get($url, [ 'http_errors' => false ]); return $response->getS
private function getStatusCode($url)
{
$response = $this->client->get($url, [
'http_errors' => false
]);
return $response->getStatusCode();
}
我试过这个代码,但没用。我需要你的帮助来解决这个问题
private function getStatusCode($url)
{
try{
$response = $this->client->get($url, [
'http_errors' => false
]);
return $response->getStatusCode();
}
//catch specific exception....
catch(QueryException $e)
{
//...and do whatever you want
return $response;
}
}
您是否尝试过在
app/Exceptions/Handler.php
中更改代码以呈现方法
public function render($request, Exception $e)
{
if($this->isHttpException($e))
{
switch ($e->getStatusCode()) {
// 404 not found
case '404':
return redirect()->guest('404pageURL');
break;
// internal error
case '500':
return redirect()->guest('500pageURL');
break;
default:
return $this->renderHttpException($e);
break;
}
}
else
{
return parent::render($request, $e);
}
}
根据您的要求更改404pageURL和500pageURL。然而,我没有测试这段代码,但它应该为您工作