user2002495
user2002495

Reputation: 2146

Laravel 4 More Efficient Routing?

I have a case of routing like this:

//  Game
Route::get('game/p/{action}', 'GameController@getPage');
Route::get('game/e/{id}', 'GameController@edit');

Route::post('game/p/add', 'GameController@add');

//  GameCategory
Route::get('gamecategory/p/{action}', 'GameCategoryController@getPage');
Route::get('gamecategory/e/{id}', 'GameCategoryController@edit');

Route::post('gamecategory/p/add', 'GameCategoryController@add');

//  Deposit
Route::get('deposit/p/{action}', 'DepositController@getPage');
Route::get('deposit/e/{id}', 'DepositController@edit');

Route::post('deposit/p/update', 'DepositController@update');

As can be seen here, the code is quite repetitive, but certain module only uses certain Controller and their route pattern is similar.

I've been googling and found that Route::resource can shorten this but I have no idea how to implement it in my case. Can someone help me? Thanks

Upvotes: 1

Views: 85

Answers (1)

Andreas
Andreas

Reputation: 8029

As your routing schema seems rather unconventional there's no helper method that could achieve what you want. What you could do is write your own helper function that takes the name of the route ('game', 'gamecategory', 'deposit') and the name of the controller ('GameController' and so on) and generates the routes you want from that.

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');

Upvotes: 1

Related Questions