/
ScaffoldCollectionController.php
52 lines (40 loc) · 1.48 KB
/
ScaffoldCollectionController.php
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
<?php
namespace Statamic\Http\Controllers\CP\Collections;
use Illuminate\Http\Request;
use Statamic\Contracts\Entries\Collection as CollectionContract;
use Statamic\Facades\File;
use Statamic\Http\Controllers\CP\CpController;
class ScaffoldCollectionController extends CpController
{
public function index($collection)
{
$this->authorize('view', $collection, __('You are not authorized to view this collection.'));
return view('statamic::collections.scaffold', compact('collection'));
}
public function create(Request $request, $collection)
{
$this->authorize('store', CollectionContract::class, __('You are not authorized to scaffold resources.'));
// Make the index template
if ($index = $this->request->get('index')) {
$this->makeTemplate($index);
}
// Make the show template
if ($show = $this->request->get('show')) {
$this->makeTemplate($show);
}
session()->flash('success', __('Views created successfully'));
return [
'redirect' => route('statamic.cp.collections.show', $request->collection->handle()),
];
}
private function makeTemplate($filename)
{
$file = resource_path("views/{$filename}.antlers.html");
// Don't overwrite existing
if (! File::get($file)) {
File::put($file, '');
}
// Set the template
$this->request->collection->template($filename)->save();
}
}