2014-04-23 47 views
1

我有路由這樣的情況:Laravel 4更有效的路由?

// Game 
Route::get('game/p/{action}', '[email protected]'); 
Route::get('game/e/{id}', '[email protected]'); 

Route::post('game/p/add', '[email protected]'); 

// GameCategory 
Route::get('gamecategory/p/{action}', '[email protected]'); 
Route::get('gamecategory/e/{id}', '[email protected]'); 

Route::post('gamecategory/p/add', '[email protected]'); 

// Deposit 
Route::get('deposit/p/{action}', '[email protected]'); 
Route::get('deposit/e/{id}', '[email protected]'); 

Route::post('deposit/p/update', '[email protected]'); 

由於可以在這裏看到,該代碼是相當重複的,但某些模塊只使用特定的控制器和他們的路由模式是類似的。

我一直在谷歌搜索,發現Route::resource可以縮短這一點,但我不知道如何實施它在我的情況。有人能幫我嗎?謝謝

回答

1

由於你的路由模式看起來相當不合常規,所以沒有幫助方法可以實現你想要的。你可以做的是編寫你自己的幫助函數,該函數採用路線的名稱('遊戲','gamecategory','存款')和控制器的名稱('GameController'等)並生成你想要的路線從那。

function _register_routes($path, $controller) 
{ 
    Route::get("{$path}/p/{action}", "{$controller}@getPage"); 
    Route::get("{$path}/e/{id}", "{$controller}@edit"); 
    Route::post("{$path}/p/add", "{$controller}@add"); 
} 

_register_routes('game', 'GameController'); 
_register_routes('gamecategory', 'GameCategoryController'); 
+0

是的這個作品,我怎麼不考慮它?謝謝 – user2002495