2014-10-18 96 views
0

我有一個基本的博客結構,比如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 
}); 

在此先感謝!

回答

0

您可以使用通配符作爲路由的第一個URI組件,然後該通配符將可用於您的控制器。這裏唯一的問題是,如果通配符是URI的第一部分,則不能使其成爲可選項;所以你需要包括非管理員路線有些前綴還有:

domain.tld/user/posts 
domain.tld/admin/posts 

如果你這樣做,那麼你可以設置路由是這樣的:

Route::pattern('admin', 'admin|user'); 
Route::resource('{admin?}/posts', 'PortalPostController'); 
Route::resource('{admin?}/polls', 'PortalPollsController'); 
... 

在你的控制器,你然後可以讀取$ admin前綴,並在__construct方法中將'index'/'show'限制作爲beforeFilter應用,前提是前綴爲'user'。 (爲了讓事情幹,你可以把這個過濾邏輯在BaseController,並讓您PortalControllers延長BaseController和呼叫parent::__construct


另一種選擇,和一個更好的,如果你可以控制你的服務器的配置,然後,普通用戶可以使用domain.tld/postswww.domain.tld/posts來訪問您的站點,而管理員可以使用admin.domain.tld/posts。您可以捕獲主機名的第一部分,然後在Contoller中將before篩選器處理爲only篩選器(如上所述)有關此事的良好討論,請參閱Dayle Rees的代碼明亮域路由:http://daylerees.com/codebright/advanced-routing