2017-03-14 97 views
0

我有一些看起來像重複的路線。laravel申請通配符路由和控制器

// Point 
    Route::get('admincp/point', '[email protected]'); 
    Route::post('admincp/point/store', '[email protected]'); 
    Route::get('admincp/point/{id}/edit', '[email protected]'); 
    Route::patch('admincp/point/{id}/update', '[email protected]'); 
    Route::get('admincp/point/{id}/destroy', '[email protected]'); 

// Faq 
    Route::get('admincp/faq', '[email protected]'); 
    Route::post('admincp/faq/store', '[email protected]'); 
    Route::get('admincp/faq/{id}/edit', '[email protected]'); 
    Route::patch('admincp/faq/{id}/update', '[email protected]'); 
    Route::get('admincp/faq/{id}/destroy', '[email protected]'); 

// Slider 
    Route::get('admincp/slider', '[email protected]'); 
    Route::post('admincp/slider/store', '[email protected]'); 
    Route::get('admincp/slider/{id}/edit', '[email protected]'); 
    Route::patch('admincp/slider/{id}/update', '[email protected]'); 
    Route::get('admincp/slider/{id}/destroy', '[email protected]'); 

我不想重複自己。我能在拉拉維爾這樣做嗎?

function generateRoute($name) { 
     Route::get("admincp/$name", "{$name}[email protected]"); 
     Route::post("admincp/$name/store", "{$name}[email protected]"); 
     Route::get("admincp/$name/{id}/edit", "{$name}[email protected]"); 
     Route::patch("admincp/$name/{id}/update", "{$name}[email protected]"); 
     Route::get("admincp/$name/{id}/destroy", "{$name}[email protected]"); 
} 

回答

1

使用資源路線:

Route::group(['prefix' => 'admincp'], function() { 
     Route::resource('point', 'PointController'); 
     Route::resource('faq', 'FaqController'); 
     Route::resource('slider', 'SliderController'); 
    } 
+0

謝謝你,我的朋友 –