我有一個基本的博客結構,比如Laravel。由於保持事物乾燥,公衆和管理共享控制器上崗,民意調查等。我這樣做,通過使用only
過濾器(我想這是一個過濾器?)在路由組中使用'唯一'路由過濾器
/*
The public can access only index and show ::resource routes
- domain.tld/posts
- domain.tld/posts/{id}
- domain.tld/polls
- domain.tld/polls/{id}
- domain.tld/categories
- domain.tld/categories/{id}
- domain.tld/tags
- domain.tld/tags/{id}
*/
Route::resource('posts', 'PortalPostsController', ['only' => ['index', 'show']]);
Route::resource('polls', 'PortalPollsController', ['only' => ['index', 'show']]);
Route::resource('categories', 'PortalCategoriesController', ['only' => ['index', 'show']]);
Route::resource('tags', 'PortalTagsController', ['only' => ['index', 'show']]);
/*
Administration uses the same PortalSomethingController but can access
all routes by prefixing administration/ to the URL:
- domain.tld/administration/posts
- domain.tld/administration/posts/create
... all of other administration/resource('posts') routes
... all administration/resource('polls') routes
... all administration/resource('categories') routes
... all administration/resource('tags') routes
*/
Route::group(['prefix' => 'administration'], function() {
Route::resource('posts', 'PortalPostsController');
Route::resource('polls', 'PortalPollsController');
Route::resource('categories', 'PortalCategoriesController');
Route::resource('tags', 'PortalTagsController');
});
現在,讓事情變得路由幹,我想避免在每個Route ::資源上定義['only'=> ['index','show']]來限制公共訪問。所以,像這樣:
// for public access
Route::group(['only' => ['index', 'show']], function() {
Route::resource('posts', 'PortalPostsController');
Route::resource('polls', 'PortallPollsController');
Route::resource('categories', 'PortallCategoriesController');
Route::resource('tags', 'PortallTagsController');
});
// for admin access
Route::group(['prefix' => 'administration'], function() {
Route::resource('posts', 'PortalPostController');
Route::resource('polls', 'PortallPollsController');
Route::resource('categories', 'PortallCategoriesController');
Route::resource('tags', 'PortallTagsController');
});
如果可能的話,我該怎麼做呢?另外,正如你所看到的,公共路由和管理路由組使用相同的資源(公衆僅限於某些路由)和相同的控制器,那麼是否有可能將其幹掉更多?例如:
// define shared resources
Route::group(['name' => 'sharedResources'], function() {
Route::resource('posts', 'PortalPostController');
Route::resource('polls', 'PortallPollsController');
Route::resource('categories', 'PortallCategoriesController');
Route::resource('tags', 'PortallTagsController');
});
// public access
Route::group(['only' => ['index', 'show']], function() {
// use sharedResources
});
// admin access
Route::group(['prefix' => 'administration'], function() {
// use sharedResources
});
在此先感謝!