عدم تكرار middlewar - إستخدم Route Groups

عدم تكرار middlewar - إستخدم Route Groups

2024-11-21 وقت القراءه : 1 دقائق

Bad Practice Example

Route::get('/productionOrders',
   [PoductionOrderController::class, 'index']
)->name('productionOrders')->middleware('RoleMeister');

Route::get('/productionOrders/delete/{id}',
   [PoductionOrderController::class, 'delete']
)->name('productionOrdersDelete')->middleware('RoleMeister');

Route::get('/productionOrders/update/{id}',
   [PoductionOrderController::class, 'createView']
)->name('productionOrdersUpdateView')->middleware('RoleMeister');

Route::post('/productionOrders/update/{id}',
   [PoductionOrderController::class, 'update']
)->name('productionOrdersUpdate')->middleware('RoleMeister');

Route::post('/productionOrders/create',
   [PoductionOrderController::class, 'create']
)->name('productionOrdersCreate')->middleware('RoleMeister');

What To Do Instead

Route::group(['middleware' => 'RoleMeister', 'prefix' => 'productionOrders'],
   function() {
        Route::get('/', [PoductionOrderController::class, 'index'])->name('productionOrders');
        Route::get('/delete/{id}', [PoductionOrderController::class, 'delete'])->name('productionOrdersDelete');
        Route::get('/update/{id}', [PoductionOrderController::class, 'createView'])->name('productionOrdersUpdateView');
        Route::post('/update/{id}', [PoductionOrderController::class, 'update'])->name('productionOrdersUpdate');
        Route::post('/create', [PoductionOrderController::class, 'create'])->name('productionOrdersCreate');
});

الـ route التي لها نفس prefix و middleware من المفضل أن يتم تجميعها معاً تحت Route::group، مما يجعلها أكثر قابلية للقراءة وكذلك حجم أقل.

 



إضافة تعليق
Loading...