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