packy

Function to Dynamically make routes

Posted 1 year ago by packy

Has anyone ever made a function to create routes? For instance I am making a custom CRUD (cant use a stock one like Backpack since its pretty complex) but all the routes for all my models follow the same structure:

in my Admin.php (for admin routes)

Route::get('coach', '[email protected]')->name('coach');
    Route::get('coach/create', '[email protected]')->name('coach.create');
    Route::post('coach/create', '[email protected]')->name('coach.store');
    Route::get('coach/{coach}/preview', '[email protected]')->name('coach.show');
    Route::get('coach/{coach}', '[email protected]')->name('coach.edit');
    Route::post('coach/{coach}/update', '[email protected]')->name('coach.update');
    Route::get('coach/{coach}/destroy', '[email protected]')->name('coach.destroy');

    Route::get('program', '[email protected]')->name('program');
    Route::get('program/create', '[email protected]')->name('program.create');
    Route::post('program/create', '[email protected]')->name('program.store');
    Route::get('program/{program}/preview', '[email protected]')->name('program.show');
    Route::get('program/{program}', '[email protected]')->name('program.edit');
    Route::post('program/{program}/update', '[email protected]')->name('program.update');
    Route::get('program/{program}/destroy', '[email protected]')->name('program.destroy');

As you can see the routes are the exact same minus coach and program. I have like 10 more models so dont want a billion routes. Seems like a good time for a function like:

makeRoute('program', 'AdminProgramController');

and it would generate all the routes. Has anyone done this? I know it could be done, just not sure where to put it or any best practice.

Please sign in or create an account to participate in this conversation.

Reply to

Use Markdown with GitHub-flavored code blocks.