Monday, March 13, 2017

laravel apply wildcard route and controller

I have some routes that look like repeated.

// Point
  Route::get('admincp/point', 'PointController@index');
  Route::post('admincp/point/store', 'PointController@store');
  Route::get('admincp/point/{id}/edit', 'PointController@edit');
  Route::patch('admincp/point/{id}/update', 'PointController@update');
  Route::get('admincp/point/{id}/destroy', 'PointController@destroy');

// Faq
  Route::get('admincp/faq', 'FaqController@index');
  Route::post('admincp/faq/store', 'FaqController@store');
  Route::get('admincp/faq/{id}/edit', 'FaqController@edit');
  Route::patch('admincp/faq/{id}/update', 'FaqController@update');
  Route::get('admincp/faq/{id}/destroy', 'FaqController@destroy');

// Slider
  Route::get('admincp/slider', 'SliderController@index');
  Route::post('admincp/slider/store', 'SliderController@store');
  Route::get('admincp/slider/{id}/edit', 'SliderController@edit');
  Route::patch('admincp/slider/{id}/update', 'SliderController@update');
  Route::get('admincp/slider/{id}/destroy', 'SliderController@destroy');

I don't want to repeat myself. Can I do in laravel something like this?

function generateRoute($name) {
      Route::get("admincp/$name", "{$name}Controller@index");
      Route::post("admincp/$name/store", "{$name}Controller@store");
      Route::get("admincp/$name/{id}/edit", "{$name}Controller@edit");
      Route::patch("admincp/$name/{id}/update", "{$name}Controller@update");
      Route::get("admincp/$name/{id}/destroy", "{$name}Controller@destroy");
}



via Ed Joe

Advertisement