回答編集履歴
2
わかりやすく変えてみました
test
CHANGED
@@ -1,8 +1,46 @@
|
|
1
|
-
`php artisan make:controller
|
1
|
+
`php artisan make:controller HogeController --resource`のコマンドでResource用のコントローラーが作成できます。
|
2
2
|
|
3
3
|
|
4
4
|
|
5
|
+
```php
|
6
|
+
|
7
|
+
<?php
|
8
|
+
|
9
|
+
|
10
|
+
|
11
|
+
namespace App\Http\Controllers;
|
12
|
+
|
13
|
+
|
14
|
+
|
15
|
+
use Illuminate\Http\Request;
|
16
|
+
|
17
|
+
|
18
|
+
|
19
|
+
class HogeController extends Controller
|
20
|
+
|
21
|
+
{
|
22
|
+
|
5
|
-
|
23
|
+
public function index() {}
|
24
|
+
|
25
|
+
public function create() {}
|
26
|
+
|
27
|
+
public function store(Request $request) {}
|
28
|
+
|
29
|
+
public function show($id) {}
|
30
|
+
|
31
|
+
public function edit($id) {}
|
32
|
+
|
33
|
+
public function update(Request $request, $id) {}
|
34
|
+
|
35
|
+
public function destroy($id) {}
|
36
|
+
|
37
|
+
}
|
38
|
+
|
39
|
+
```
|
40
|
+
|
41
|
+
|
42
|
+
|
43
|
+
`web.php`に`Route::resource('hoges', 'HogeController');`を追加したら、上記のコントローラーのメソッドが下記のテーブルにあるURIにマッピングされます。
|
6
44
|
|
7
45
|
|
8
46
|
|
@@ -10,16 +48,40 @@
|
|
10
48
|
|
11
49
|
|:--|:--|:--|:--|
|
12
50
|
|
13
|
-
|GET|/
|
51
|
+
|GET|/hoges|index|hoges.index|
|
14
52
|
|
15
|
-
GET|/
|
53
|
+
GET|/hoges/create|create|hoges.create
|
16
54
|
|
17
|
-
POST|/
|
55
|
+
POST|/hoges|store|hoges.store
|
18
56
|
|
19
|
-
GET|/
|
57
|
+
GET|/hoges/{hoge}|show|hoges.show
|
20
58
|
|
21
|
-
GET|/
|
59
|
+
GET|/hoges/{hoge}/edit|edit|hoges.edit
|
22
60
|
|
23
|
-
PUT/PATCH|/
|
61
|
+
PUT/PATCH|/hoges/{hoge}|update|hoges.update
|
24
62
|
|
25
|
-
DELETE|/
|
63
|
+
DELETE|/hoges/{hoge}|destroy|hoges.destroy
|
64
|
+
|
65
|
+
|
66
|
+
|
67
|
+
リソースの下に、リソースを作ることも可能です。
|
68
|
+
|
69
|
+
`web.php`に`Route::resource('hoges.hares', 'HareController');`。
|
70
|
+
|
71
|
+
|
72
|
+
|
73
|
+
|
74
|
+
|
75
|
+
すべてのメソッドを使わない場合は、ルートに指定することも可能です。
|
76
|
+
|
77
|
+
```php
|
78
|
+
|
79
|
+
Route::resource('hoge', 'HogeController', ['only' => ['index', 'show']]); // indexとshowのみ
|
80
|
+
|
81
|
+
Route::resource('hoge', 'HogeController', ['except' => ['destroy']]); // destroy以外
|
82
|
+
|
83
|
+
```
|
84
|
+
|
85
|
+
|
86
|
+
|
87
|
+
このパターンと離れた設定は一つずつ指定した方が良さそうかもしれません。
|
1
ヘッダーを日本語に
test
CHANGED
@@ -6,7 +6,7 @@
|
|
6
6
|
|
7
7
|
|
8
8
|
|
9
|
-
|Verb|URI|
|
9
|
+
|HTTP Verb|URI|コントローラーでのメソッド名|ルート名|
|
10
10
|
|
11
11
|
|:--|:--|:--|:--|
|
12
12
|
|