似たような例としてコントローラのルーティングがあります。
php
1Route::get('posts', 'PostController@index');
コントローラの場合は
laravel/RouteServiceProvider.php at d86dd9b1eccbfea9c37205129c79e9fa9c376a76 · laravel/laravel
ここでベースとなるネームスペースを定義できるのですが,ポリシー解決に関するコードを読んでみてもそういった処理は見当たりませんでした。ドキュメントが不適切である可能性がありますね。
framework/Gate.php at 6a64c8d37143322c5a02b978c499d0e2b303c6f6 · laravel/framework
framework/Gate.php at 6a64c8d37143322c5a02b978c499d0e2b303c6f6 · laravel/framework
framework/Str.php at 6a64c8d37143322c5a02b978c499d0e2b303c6f6 · laravel/framework
framework/Gate.php at 6a64c8d37143322c5a02b978c499d0e2b303c6f6 · laravel/framework
蛇足ですが,ポリシーのチェックはコントローラで明示的に呼び出すよりも,can
ミドルウェアを使ってコントローラメソッドに突入する前に自動的に呼び出させるほうがおすすめです。(ルートモデルバインディングで取ってこれない情報がある場合は無理ですが)
php
1Route::get('posts', 'PostController@index')->middleware('can:index,App\Post')->name('posts.index');
2Route::get('posts/{post}', 'PostController@show')->middleware('can:show,post')->name('posts.show');
3Route::get('communities/{community}/posts', 'PostController@indexOfCommunity')->middleware('can:index,App\Post,community')->name('communities.posts.index');
php
1use Illuminate\Http\Request;
2use App\Post;
3use App\Community;
4
5class PostController
6{
7 public function index(Request $request) { }
8 public function show(Request $request, Post $post) { }
9 public function indexOfCommunity(Request $request, Community $community) { }
10}
php
1use App\User;
2use App\Post;
3use App\Community;
4
5class PostPolicy
6{
7 public function index(User $user) { }
8 public function show(User $user, Post $post) { }
9 public function indexOfCommunity(User $user, Community $community) { }
10}
※ ->name(...)
は php artisan route:list
したときに分かりやすくなるように付けているだけです
【おまけ】
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/02/17 09:08