Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/289.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Php laravel:异常处理_Php_Laravel - Fatal编程技术网

Php laravel:异常处理

Php laravel:异常处理,php,laravel,Php,Laravel,我有这个功能,它能够获得网址的状态码,如果提供了一个假网址,它只是通过说“假网址”的异常。我想处理这个异常,并将其视为404,该网站已关闭 private function getStatusCode($url) { $response = $this->client->get($url, [ 'http_errors' => false ]); return $response->getS

我有这个功能,它能够获得网址的状态码,如果提供了一个假网址,它只是通过说“假网址”的异常。我想处理这个异常,并将其视为404,该网站已关闭

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);
  }
}
根据您的要求更改404pageURL500pageURL。然而,我没有测试这段代码,但它应该为您工作