sajadsdi/laravel-dynamic-router
Installation
composer require sajadsdi/laravel-dynamic-router
Publish the config file:
php artisan vendor:publish --provider="Sajadsdi\DynamicRouter\DynamicRouterServiceProvider" --tag="config"
Define Routes in Config
Edit config/dynamic-router.php to define your routes:
'routes' => [
'api' => [
'users' => [
'get' => [
'path' => '/users',
'controller' => 'App\Http\Controllers\UserController@index',
'middleware' => ['api', 'auth:sanctum'],
],
'post' => [
'path' => '/users',
'controller' => 'App\Http\Controllers\UserController@store',
],
],
],
],
Register the Provider
Add to config/app.php under providers:
Sajadsdi\DynamicRouter\DynamicRouterServiceProvider::class,
First Use Case Run the route registration command:
php artisan dynamic:routes
Verify routes are registered by checking php artisan route:list.
Config-Driven Development
Store routes in config/dynamic-router.php for easy maintenance and version control.
Example:
'admin' => [
'dashboard' => [
'get' => [
'path' => '/admin/dashboard',
'controller' => 'App\Http\Controllers\Admin\DashboardController@index',
'middleware' => ['web', 'auth:admin'],
],
],
],
Integration with Existing Routes Use the package alongside Laravel’s native routes. Dynamic routes can be grouped under a prefix:
'routes' => [
'api/v1' => [
'posts' => [
'get' => [
'path' => '/posts',
'controller' => 'App\Http\Controllers\Api\PostController@index',
],
],
],
],
Conditional Route Registration
Leverage the register method in a service provider to conditionally load routes:
if ($this->app->environment('production')) {
DynamicRouter::register('production-routes');
}
Reusable Route Groups Define reusable middleware or namespace groups in the config:
'groups' => [
'api' => [
'middleware' => ['api', 'throttle:60,1'],
'namespace' => 'App\Http\Controllers\Api',
],
],
Route Caching Cache routes for performance in production:
php artisan dynamic:routes --cache
Route Overwriting
Dynamic routes overwrite existing routes with the same URI. Use unique paths or clear the route cache (php artisan route:clear) if conflicts arise.
Middleware Not Loaded
Ensure middleware defined in the config exists in app/Http/Kernel.php. Missing middleware will cause runtime errors.
Controller Namespace Issues
If controllers are not found, verify the namespace in config/dynamic-router.php matches your App\Http\Controllers namespace or adjust accordingly.
Route Cache Stale Data
After modifying config/dynamic-router.php, always run:
php artisan route:clear
php artisan dynamic:routes
Check Registered Routes
Use php artisan route:list to verify routes are registered correctly. Filter by name or URI:
php artisan route:list --name="users.index"
Enable Route Debugging
Temporarily disable route caching in config/dynamic-router.php:
'cache' => env('APP_DEBUG', false),
Log Route Registration
Add logging to the DynamicRouterServiceProvider to track registration issues:
protected function registerRoutes()
{
\Log::info('Registering dynamic routes...');
// Existing code
}
Custom Route Registration Logic
Override the register method in a subclass of DynamicRouterServiceProvider:
class CustomDynamicRouterServiceProvider extends DynamicRouterServiceProvider
{
public function register()
{
if (someCondition()) {
$this->mergeConfigFrom(__DIR__.'/custom-routes.php', 'dynamic-router');
}
parent::register();
}
}
Dynamic Route Generation Use the package to generate API documentation (e.g., Swagger/OpenAPI) by parsing the config:
$routes = config('dynamic-router.routes');
// Process $routes to generate OpenAPI specs
Environment-Specific Routes Load different route configs based on the environment:
$this->mergeConfigFrom(__DIR__.'/routes-'.config('app.env').'.php', 'dynamic-router');
Validation Validate route configurations before registration using Laravel’s validation rules:
use Illuminate\Support\Facades\Validator;
$validator = Validator::make($routeConfig, [
'path' => 'required|string',
'controller' => 'required|string',
'middleware' => 'sometimes|array',
]);
How can I help you explore Laravel packages today?