Installation
composer require doppy/routing-bundle
Add to config/app.php under providers:
Doppy\RoutingBundle\RoutingServiceProvider::class,
First Use Case: Route Grouping
Define a route group in routes/web.php:
use Doppy\RoutingBundle\Routing;
Route::group(['prefix' => 'admin', 'middleware' => ['auth']], function () {
Routing::group(['prefix' => 'users'], function () {
Route::get('/', 'UserController@index');
Route::post('/create', 'UserController@store');
});
});
Routing facade simplifies nested route grouping with cleaner syntax.Useful for multi-tenant or locale-based routing:
$tenantId = Auth::user()->tenant_id;
Routing::group(['prefix' => "tenant/{$tenantId}"], function () {
Route::get('/dashboard', 'DashboardController@show');
});
Combine middleware with route groups:
Routing::group([
'prefix' => 'api/v1',
'middleware' => ['throttle:60', 'api.auth']
], function () {
Route::get('/posts', 'PostController@index');
});
Attach routes to controllers dynamically:
class AdminController extends Controller {
public function routes() {
return [
'get.index' => ['path' => '/dashboard', 'as' => 'admin.dashboard'],
'post.store' => ['path' => '/create', 'as' => 'admin.create']
];
}
}
Register in a service provider:
public function boot() {
Routing::registerControllerRoutes(AdminController::class);
}
Generate RESTful routes for resources:
Routing::resource('posts', 'PostController')
->only(['index', 'store'])
->middleware('auth:admin');
Facade vs. Direct Usage
Routing facade is a wrapper around Laravel’s Route service. Overusing it may obscure Laravel’s native features (e.g., Route::prefix()).Route:: for complex cases.Route Caching Conflicts
{tenantId}) break route caching. Exclude dynamic groups from caching:
Route::group(['prefix' => 'dynamic'], function () {
// ...
})->where('tenantId', '[0-9]+')->ignoreRouteCache();
Controller Route Registration
routes() method in controllers returns an array with valid keys (get.index, post.store). Invalid keys throw silent errors.dd(Routing::getRegisteredRoutes(AdminController::class)) to verify.Extend the Facade
Add custom methods to the Routing facade for project-specific needs:
// In a service provider:
Facades\Routing::extend('api', function () {
return new class {
public function group($callback) {
return Route::prefix('api/v2')->group($callback);
}
};
});
Usage:
Routing::api()->group(function () { ... });
Debugging Route Groups Dump the compiled route list to inspect groups:
dd(Route::getRoutes()->getByName());
Performance
Route::group() for simple cases to avoid facade overhead.Routing::group() only when nesting exceeds 3 levels (improves readability).Integration with Laravel Scout Dynamically generate search routes:
Routing::group(['prefix' => 'search'], function () {
Route::get('/{model}', 'SearchController@index')
->where('model', implode('|', ['posts', 'users']));
});
How can I help you explore Laravel packages today?