Weave Code
Code Weaver
Helps Laravel developers discover, compare, and choose open-source packages. See popularity, security, maintainers, and scores at a glance to make better decisions.
Feedback
Share your thoughts, report bugs, or suggest improvements.
Subject
Message

Laravel Dynamic Router Laravel Package

sajadsdi/laravel-dynamic-router

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require sajadsdi/laravel-dynamic-router
    

    Publish the config file:

    php artisan vendor:publish --provider="Sajadsdi\DynamicRouter\DynamicRouterServiceProvider" --tag="config"
    
  2. 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',
                ],
            ],
        ],
    ],
    
  3. Register the Provider Add to config/app.php under providers:

    Sajadsdi\DynamicRouter\DynamicRouterServiceProvider::class,
    
  4. First Use Case Run the route registration command:

    php artisan dynamic:routes
    

    Verify routes are registered by checking php artisan route:list.


Implementation Patterns

Workflow: Dynamic Route Management

  1. 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'],
            ],
        ],
    ],
    
  2. 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',
                ],
            ],
        ],
    ],
    
  3. Conditional Route Registration Leverage the register method in a service provider to conditionally load routes:

    if ($this->app->environment('production')) {
        DynamicRouter::register('production-routes');
    }
    
  4. Reusable Route Groups Define reusable middleware or namespace groups in the config:

    'groups' => [
        'api' => [
            'middleware' => ['api', 'throttle:60,1'],
            'namespace' => 'App\Http\Controllers\Api',
        ],
    ],
    
  5. Route Caching Cache routes for performance in production:

    php artisan dynamic:routes --cache
    

Gotchas and Tips

Pitfalls

  1. 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.

  2. Middleware Not Loaded Ensure middleware defined in the config exists in app/Http/Kernel.php. Missing middleware will cause runtime errors.

  3. 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.

  4. Route Cache Stale Data After modifying config/dynamic-router.php, always run:

    php artisan route:clear
    php artisan dynamic:routes
    

Debugging Tips

  1. 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"
    
  2. Enable Route Debugging Temporarily disable route caching in config/dynamic-router.php:

    'cache' => env('APP_DEBUG', false),
    
  3. Log Route Registration Add logging to the DynamicRouterServiceProvider to track registration issues:

    protected function registerRoutes()
    {
        \Log::info('Registering dynamic routes...');
        // Existing code
    }
    

Extension Points

  1. 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();
        }
    }
    
  2. 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
    
  3. Environment-Specific Routes Load different route configs based on the environment:

    $this->mergeConfigFrom(__DIR__.'/routes-'.config('app.env').'.php', 'dynamic-router');
    
  4. 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',
    ]);
    
Weaver

How can I help you explore Laravel packages today?

Conversation history is not saved when not logged in.
Prompt
Add packages to context
No packages found.
directorytree/privacy-filter-classifier
directorytree/privacy-filter
babenkoivan/elastic-client
innmind/static-analysis
innmind/coding-standard
datacore/hub-sdk
develia/commons
cuci/prototurk-sdk
cuci/prototurk-sdk-symfony
develia/geo-bundle
dreamzy/livewire-charts
touchestate-sdk/php-sdk
22h/doctrine-garbage-collection-bundle
agtp/agtp-php
agtp/mod-php
splash/sonata-admin
splash/metadata
splash/openapi
splash/scopes
splash/toolkit