Skip to content
This repository was archived by the owner on Feb 23, 2021. It is now read-only.

Commit 4ffd49d

Browse files
committed
Random cat in error page
Close #10
1 parent 4911e77 commit 4ffd49d

File tree

4 files changed

+53
-2
lines changed

4 files changed

+53
-2
lines changed

.env.example

+6
Original file line numberDiff line numberDiff line change
@@ -23,3 +23,9 @@ SMTP_HOST=smtp.mailgun.org
2323
SMTP_PORT=587
2424
SMTP_USERNAME=(null)
2525
SMTP_PASSWORD=(null)
26+
27+
### Pixabay API設定
28+
### 請至網站註冊取得,否則沒有貓貓圖
29+
### http://pixabay.com/api/docs/
30+
PIXABAY_USERNAME=(null)
31+
PIXABAY_KEY=(null)

app/Cat.php

+38
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,38 @@
1+
<?php namespace App;
2+
3+
use GuzzleHttp\Client;
4+
use Illuminate\Database\Eloquent\Model;
5+
6+
class Cat extends Model
7+
{
8+
9+
/*
10+
* 隨機貓貓圖
11+
*/
12+
public static function random()
13+
{
14+
$username = env('PIXABAY_USERNAME');
15+
$key = env('PIXABAY_KEY');
16+
//檢查username與key
17+
if (empty($username) || empty($key)) {
18+
return null;
19+
}
20+
//抓取最新幾張圖,從中隨機(5-200)
21+
$number = 50;
22+
//API請求
23+
$client = new Client();
24+
$options = array(
25+
'username' => $username,
26+
'key' => $key,
27+
'image_type' => 'photo',
28+
'min_width' => '600',
29+
'q' => 'cute+cat',
30+
'per_page' => $number,
31+
'order' => 'latest'
32+
);
33+
$response = $client->get('http://pixabay.com/api', ['query' => $options]);
34+
$json = $response->json();
35+
return $json["hits"][rand(0, $number - 1)]["webformatURL"];
36+
}
37+
38+
}

app/Exceptions/Handler.php

+4-1
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
<?php namespace App\Exceptions;
22

3+
use App\Cat;
34
use Exception;
45
use Illuminate\Foundation\Exceptions\Handler as ExceptionHandler;
56
use Symfony\Component\Debug\ExceptionHandler as SymfonyDisplayer;
@@ -115,9 +116,11 @@ protected function renderHttpException(HttpException $e)
115116
);
116117

117118
if (array_key_exists($status, $http_codes)) {
119+
$pic = Cat::random();
118120
$error = array(
119121
'code' => $status,
120-
'message' => $http_codes[$status]
122+
'message' => $http_codes[$status],
123+
'pic' => $pic
121124
);
122125
return response()->view("errors.error", $error, $status);
123126
} else {

resources/views/errors/error.blade.php

+5-1
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,11 @@
77
@section('content')
88
<div class="container">
99
<div class="row text-center">
10-
{!! HTML::image('pic/'. $code . '.jpg','Bad Request'); !!}
10+
<h1>{{ $code }}</h1>
11+
<h1>{{ $message }}</h1>
12+
@if(!empty($pic))
13+
{!! HTML::image($pic, $message, ['width' => '600px']); !!}
14+
@endif
1115
</div>
1216
</div>
1317
@endsection

0 commit comments

Comments
 (0)