teratail header banner
teratail header banner
質問するログイン新規登録

回答編集履歴

2

わかりやすく変えてみました

2019/02/08 02:56

投稿

Bremenkanp
Bremenkanp

スコア205

answer CHANGED
@@ -1,13 +1,44 @@
1
- `php artisan make:controller PhotoController --resource`のコマンドでResource用のコントローラーが作成できます。
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|/photos|index|photos.index|
26
+ |GET|/hoges|index|hoges.index|
8
- GET|/photos/create|create|photos.create
27
+ GET|/hoges/create|create|hoges.create
9
- POST|/photos|store|photos.store
28
+ POST|/hoges|store|hoges.store
10
- GET|/photos/{photo}|show|photos.show
29
+ GET|/hoges/{hoge}|show|hoges.show
11
- GET|/photos/{photo}/edit|edit|photos.edit
30
+ GET|/hoges/{hoge}/edit|edit|hoges.edit
12
- PUT/PATCH|/photos/{photo}|update|photos.update
31
+ PUT/PATCH|/hoges/{hoge}|update|hoges.update
13
- DELETE|/photos/{photo}|destroy|photos.destroy
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

ヘッダーを日本語に

2019/02/08 02:56

投稿

Bremenkanp
Bremenkanp

スコア205

answer CHANGED
@@ -2,7 +2,7 @@
2
2
 
3
3
  ルートが下記のようになります。
4
4
 
5
- |Verb|URI|Action|Route Name|
5
+ |HTTP Verb|URI|コントローラーでのメソッド名|ルート名|
6
6
  |:--|:--|:--|:--|
7
7
  |GET|/photos|index|photos.index|
8
8
  GET|/photos/create|create|photos.create