回答編集履歴

3

修正

2018/02/11 10:13

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -4,7 +4,7 @@
4
4
 
5
5
  ```php
6
6
 
7
- Route::get('/posts', 'PostController@index');
7
+ Route::get('posts', 'PostController@index');
8
8
 
9
9
  ```
10
10
 
@@ -42,9 +42,9 @@
42
42
 
43
43
  ```php
44
44
 
45
- Route::get('/posts', 'PostController@index')->middleware('can:index,App\Post')->name('posts.index');
45
+ Route::get('posts', 'PostController@index')->middleware('can:index,App\Post')->name('posts.index');
46
46
 
47
- Route::get('/posts/{post}', 'PostController@show')->middleware('can:show,post')->name('posts.show');
47
+ Route::get('posts/{post}', 'PostController@show')->middleware('can:show,post')->name('posts.show');
48
48
 
49
49
  Route::get('communities/{community}/posts', 'PostController@indexOfCommunity')->middleware('can:index,App\Post,community')->name('communities.posts.index');
50
50
 

2

補足

2018/02/11 10:12

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -46,7 +46,65 @@
46
46
 
47
47
  Route::get('/posts/{post}', 'PostController@show')->middleware('can:show,post')->name('posts.show');
48
48
 
49
+ Route::get('communities/{community}/posts', 'PostController@indexOfCommunity')->middleware('can:index,App\Post,community')->name('communities.posts.index');
50
+
49
51
  ```
52
+
53
+
54
+
55
+ ```php
56
+
57
+ use Illuminate\Http\Request;
58
+
59
+ use App\Post;
60
+
61
+ use App\Community;
62
+
63
+
64
+
65
+ class PostController
66
+
67
+ {
68
+
69
+ public function index(Request $request) { }
70
+
71
+ public function show(Request $request, Post $post) { }
72
+
73
+ public function indexOfCommunity(Request $request, Community $community) { }
74
+
75
+ }
76
+
77
+ ```
78
+
79
+
80
+
81
+ ```php
82
+
83
+ use App\User;
84
+
85
+ use App\Post;
86
+
87
+ use App\Community;
88
+
89
+
90
+
91
+ class PostPolicy
92
+
93
+ {
94
+
95
+ public function index(User $user) { }
96
+
97
+ public function show(User $user, Post $post) { }
98
+
99
+ public function indexOfCommunity(User $user, Community $community) { }
100
+
101
+ }
102
+
103
+ ```
104
+
105
+
106
+
107
+ ※ `->name(...)` は `php artisan route:list` したときに分かりやすくなるように付けているだけです
50
108
 
51
109
 
52
110
 

1

追記

2018/02/11 10:11

投稿

mpyw
mpyw

スコア5223

test CHANGED
@@ -47,3 +47,13 @@
47
47
  Route::get('/posts/{post}', 'PostController@show')->middleware('can:show,post')->name('posts.show');
48
48
 
49
49
  ```
50
+
51
+
52
+
53
+ 【おまけ】
54
+
55
+
56
+
57
+ - [【Laravel】Route Model Binding と Policy だけで認可処理を完結させるための Resourceful ルーティングの工夫 - Qiita](https://qiita.com/mpyw/items/13b04f80f6355b30215a)
58
+
59
+ - [[Laravel] ルーティングサービスを継承する - Qiita](https://qiita.com/mpyw/items/5d6e992fb3c40c63942f)