Route::macro('morph', function($models, $callable) {
foreach($models as $model) {
$name = class_basename($model);
$base = Str::plural($name)."/{$name}";
Route::prefix($base)->name("$name.")->group($callable);
}
});
Route::morph([ \App\User::class ], function() {
Route::get('comments', 'CommentController@index')->name('comments.index');
});
Produces /users/1/comments
/ user.comments.index
Useful to store morphable models in a package's config
-
"like" package users/{user}/likes
-
"comment" package users/{user}/comments