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 Database Routes Laravel Package

pebblecms/laravel-database-routes

View on GitHub
Deep Wiki
Context7

Getting Started

Minimal Setup

  1. Installation

    composer require pebblecms/laravel-database-routes
    

    Publish the migration and config:

    php artisan vendor:publish --provider="PebbleCMS\DatabaseRoutes\ServiceProvider" --tag="migrations"
    php artisan vendor:publish --provider="PebbleCMS\DatabaseRoutes\ServiceProvider" --tag="config"
    

    Run the migration:

    php artisan migrate
    
  2. First Use Case Define a route in the database via database_routes table:

    INSERT INTO database_routes (name, uri, action, middleware, priority)
    VALUES ('admin.dashboard', '/admin', 'AdminController@index', 'auth', 10);
    

    Clear route cache to apply changes:

    php artisan route:clear
    
  3. Where to Look First

    • Config: config/database-routes.php (adjust table name, caching behavior).
    • Service Provider: app/Providers/DatabaseRoutesServiceProvider.php (register custom route models).
    • Migration: database/migrations/[timestamp]_create_database_routes_table.php (extend with custom fields).

Implementation Patterns

Core Workflow

  1. Define Routes in Database Use the database_routes table to store routes dynamically:

    // Example: Programmatically add a route
    \PebbleCMS\DatabaseRoutes\Facades\DatabaseRoutes::add([
        'name' => 'api.user.profile',
        'uri' => 'api/user/profile',
        'action' => 'App\Http\Controllers\UserController@show',
        'middleware' => 'api,auth:sanctum',
        'priority' => 20,
    ]);
    
  2. Integrate with Existing Routes Combine database routes with static routes in routes/web.php or routes/api.php:

    // routes/web.php
    Route::get('/static', function () { return 'Static Route'; });
    
    // Database routes (auto-loaded after `route:clear`)
    Route::get('/dynamic', 'DynamicController@index');
    
  3. Middleware and Groups Use middleware columns to attach groups:

    -- Example: Route group with middleware
    INSERT INTO database_routes (name, uri, action, middleware, priority)
    VALUES ('admin.*', 'admin/{controller}', 'AdminController@__invoke', 'web,auth', 5);
    

    Note: Supports Laravel’s middleware syntax (e.g., auth:sanctum|api).

  4. Caching and Performance Disable caching during development (set cache_enabled to false in config). Clear cache after changes:

    php artisan route:clear
    
  5. Custom Route Models Extend the default Route model by binding a custom Eloquent model:

    // app/Providers/DatabaseRoutesServiceProvider.php
    public function boot()
    {
        DatabaseRoutes::extend(function ($app) {
            return new \App\Models\CustomRoute();
        });
    }
    

Gotchas and Tips

Pitfalls

  1. Route Cache Invalidation

    • Issue: Changes to database_routes won’t reflect until route:clear is run.
    • Fix: Automate cache clearing post-deployment or use php artisan route:clear --force in CI/CD.
  2. Name Collisions

    • Issue: Duplicate route names (e.g., admin.dashboard) will cause conflicts.
    • Fix: Use unique names or validate in a model observer:
      // app/Observers/RouteObserver.php
      public function saving(Route $route)
      {
          if (Route::named($route->name)->count()) {
              throw new \Exception("Route name {$route->name} already exists.");
          }
      }
      
  3. Middleware Parsing Quirks

    • Issue: Complex middleware (e.g., throttle:60,1) may not parse correctly.
    • Fix: Use pipe (|) syntax or simplify middleware in the database.
  4. Priority Conflicts

    • Issue: Routes with the same priority may load unpredictably.
    • Fix: Use distinct priorities (e.g., 10, 20, 30) or sort alphabetically in the model.
  5. Dynamic URI Validation

    • Issue: URIs with placeholders (e.g., /user/{id}) may not validate properly.
    • Fix: Use Laravel’s Route::hasRoute() or custom validation in a model observer.

Debugging Tips

  • Inspect Loaded Routes:
    php artisan route:list --path=/admin
    
  • Check Database Routes:
    SELECT * FROM database_routes WHERE uri LIKE '%admin%';
    
  • Enable Query Logging: Add to config/database.php:
    'log_queries' => true,
    

Extension Points

  1. Custom Route Actions Override the default action resolver to support closures or controller methods dynamically:

    // app/Providers/DatabaseRoutesServiceProvider.php
    DatabaseRoutes::extend(function ($app) {
        $app->bind('route.action.resolver', function () {
            return new \App\Services\CustomRouteActionResolver();
        });
    });
    
  2. Route Events Listen for route changes via events:

    // app/Providers/EventServiceProvider.php
    protected $listen = [
        \PebbleCMS\DatabaseRoutes\Events\RouteAdded::class => [
            \App\Listeners\LogRouteChange::class,
        ],
    ];
    
  3. API for Route Management Expose CRUD endpoints for routes:

    // routes/api.php
    Route::apiResource('routes', \App\Http\Controllers\RouteController::class);
    
  4. Soft Deletes Enable soft deletes in the model to "disable" routes without removing them:

    // app/Models/Route.php
    use SoftDeletes;
    protected $dates = ['deleted_at'];
    
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.
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
testo/output-teamcity
testo/bridge-symfony
spatie/flare-daemon-runtime
canaltp/sam-ecore-application-manager-bundle
canaltp/sam-ecore-security-manager-bundle