使用 Laravel 5.7 實作「相片藝廊 API」應用程式

設定

修改 .env 檔:

1
FILESYSTEM_DRIVER=public

建立軟連結:

1
php artisan storage:link

路由

1
2
3
Route::get('/gallery/categories', 'Gallery\CategoryController@index');
Route::get('/gallery/albums', 'Gallery\AlbumController@index');
Route::get('/gallery/photos', 'Gallery\PhotoController@index');

類別控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace App\Http\Controllers\Api\Gallery;

use Cache;
use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AlbumController extends Controller
{
protected $request;

public function __construct(Request $request)
{
$this->request = $request;
}

public function index()
{
// 路徑
$resource = implode('/', [
'images',
'web',
$this->request->category,
]);

// 快取分鐘
$minutes = config('default.cache.minutes.albums');

$albums = Cache::remember($resource, $minutes, function () use ($resource) {
$directories = array_map('basename', Storage::directories($resource));

$explode = function($self) {
$array = explode('_', $self);

return [
'date' => $array[0],
'title' => $array[1],
'subtitle' => $array[2] ?? null,
];
};

return array_map($explode, $directories);
});

return response([
'data' => $albums,
], 200);
}
}

相簿控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
namespace App\Http\Controllers\Api\Gallery;

use Cache;
use Storage;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class AlbumController extends Controller
{
protected $request;

public function __construct(Request $request)
{
$this->request = $request;
}

public function index()
{
// 路徑
$resource = implode('/', [
'images',
'web',
$this->request->category,
]);

// 快取分鐘
$minutes = config('default.cache.minutes.albums');

$albums = Cache::remember($resource, $minutes, function () use ($resource) {
$directories = array_map('basename', Storage::directories($resource));

$explode = function($self) {
$array = explode('_', $self);

return [
'date' => $array[0],
'title' => $array[1],
'subtitle' => $array[2] ?? null,
];
};

return array_map($explode, $directories);
});

return response([
'data' => $albums,
], 200);
}
}

相片控制器

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
namespace App\Http\Controllers\Api\Gallery;

use Cache;
use Storage;
use App\Helpers\Link;
use Illuminate\Http\Request;
use App\Http\Controllers\Controller;

class PhotoController extends Controller
{
protected $request;

public function __construct(Request $request)
{
$this->request = $request;
}

public function index()
{
// 路徑
$resource = implode('/', [
'images',
'web',
$this->request->category,
$this->request->album,
]);

// 快取分鐘
$minutes = config('default.cache.minutes.photos');

$photos = Cache::remember($resource, $minutes, function () use ($resource) {
$explode = function($self) {
$array = explode('/', str_replace('images/web/', '', $self));

$params = config('app.url') . '/gallery/photos?' . http_build_query([
'category' => $array[0],
'album' => $array[1],
'photo' => $array[2],
]);

return [
'name' => basename($self, '.jpg'),
'path' => [
'web' => $params . '&size=web',
'raw' => $params . '&size=raw',
'download' => $params . '&size=raw&download=true',
'share' => Link::shorten($params . '&size=raw'),
],
];
};

return array_map($explode, Storage::files($resource));
});

return response([
'data' => $photos,
], 200);
}
}

程式碼