Question

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

Was it helpful?

Solution

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');
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top