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

Symfony Route Usage Laravel Package

50bhan/symfony-route-usage

View on GitHub
Deep Wiki
Context7

Technical Evaluation

Architecture Fit

  • Symfony Focus: This package is explicitly designed for Symfony, not Laravel. While the concept (tracking route usage) is similar to Laravel’s route-usage, the implementation is framework-specific and relies on Symfony’s kernel, routing system, and event listeners.
  • Laravel Adaptability: Laravel’s routing and middleware stack differs significantly from Symfony’s. Key dependencies (e.g., Symfony’s Router, EventDispatcher) are incompatible without abstraction layers (e.g., Symfony Bridge or custom middleware).
  • Core Functionality: The package tracks route hits via a database table (route_usage), which could theoretically be replicated in Laravel, but the event-driven architecture (e.g., KernelEvents::REQUEST) and route resolution hooks would require custom Laravel middleware or service providers.

Integration Feasibility

  • High Effort: Direct integration would require:
    • Rewriting Symfony-specific event listeners for Laravel’s Bootstrap/Middleware pipeline.
    • Adapting the database schema (e.g., route_usage table) to Laravel’s migrations.
    • Replacing Symfony’s Router dependency with Laravel’s Router (e.g., via facades or interfaces).
  • Alternative Approaches:
    • Leverage Existing Laravel Packages: Prioritize spatie/laravel-route-usage (if available) or nWidart/laravel-routes for route analysis.
    • Custom Middleware: Build a lightweight Laravel middleware to log route hits to a database table, then query it via Artisan commands.
  • Database Schema: The package uses a simple route_usage table with columns like path, method, last_used_at, and count. Laravel’s migration system could accommodate this with minimal changes.

Technical Risk

  • Framework Lock-in: Symfony’s Container, EventDispatcher, and Router are tightly coupled. Porting this to Laravel introduces high refactoring risk without a clear abstraction strategy.
  • Performance Overhead: The package adds a database write per request. In Laravel, this could impact performance if not optimized (e.g., batching writes or using a queue).
  • Testing Complexity: Ensuring the middleware/service provider works across Laravel’s lifecycle (e.g., handling middleware groups, subdomains, or dynamic routes) would require extensive testing.
  • Maintenance Burden: Future updates to Symfony’s routing system could break compatibility, requiring ongoing Laravel-specific patches.

Key Questions

  1. Why Symfony-Specific?

    • Is there a business requirement to replicate Symfony’s exact behavior in Laravel, or is the goal simply to identify unused routes?
    • Are there existing Laravel packages that already solve this (e.g., Spatie’s route-usage)?
  2. Database Strategy

    • How will the route_usage table be managed in Laravel (e.g., migrations, seeding)?
    • Should route hits be logged in real-time, or is a sampled/batched approach acceptable?
  3. Integration Scope

    • Will this replace an existing analytics or monitoring system?
    • Are there API or frontend dependencies (e.g., displaying dead routes in an admin panel)?
  4. Performance Trade-offs

    • What is the acceptable latency impact of logging every route hit?
    • Could a read-only replica database be used to offload writes?
  5. Long-Term Viability

    • Is the team willing to maintain a custom Laravel port of this package, or should a community-driven solution (e.g., a fork) be pursued?

Integration Approach

Stack Fit

  • Laravel Compatibility: Low to Medium

    • Pros:
      • Laravel’s middleware and service provider systems can host custom route-tracking logic.
      • Database schema is simple and portable.
    • Cons:
      • No native Symfony integration (e.g., Router events, Container).
      • Potential conflicts with Laravel’s routing cache or optimized bootstrapping.
  • Recommended Stack:

    Component Laravel Equivalent Notes
    Symfony Router Laravel’s Illuminate\Routing\Router Use facades or interfaces for abstraction.
    Event Listeners Laravel Middleware (HandleRequests) Replace KernelEvents with middleware.
    Database Table Laravel Migration + Model (route_usage) Add indexes for path and method.
    Artisan Command Custom Artisan Command Extend Illuminate\Console\Command.

Migration Path

  1. Assessment Phase:

    • Audit existing Laravel route structure (e.g., php artisan route:list).
    • Identify critical routes that must be excluded from tracking (e.g., health checks, webhooks).
  2. Proof of Concept (PoC):

    • Build a minimal middleware to log route hits to a temporary table.
    • Test with a subset of routes to validate performance and accuracy.
    • Example middleware:
      namespace App\Http\Middleware;
      use Closure;
      use Illuminate\Support\Facades\DB;
      use Illuminate\Support\Facades\Request;
      
      class TrackRouteUsage
      {
          public function handle($request, Closure $next)
          {
              $response = $next($request);
              DB::table('route_usage')->updateOrInsert(
                  ['path' => $request->path(), 'method' => $request->method()],
                  ['last_used_at' => now(), 'count' => DB::raw('count + 1')]
              );
              return $response;
          }
      }
      
    • Register middleware in app/Http/Kernel.php (globally or for specific routes).
  3. Database Setup:

    • Create a migration for the route_usage table:
      Schema::create('route_usage', function (Blueprint $table) {
          $table->string('path');
          $table->string('method');
          $table->timestamp('last_used_at')->nullable();
          $table->integer('count')->default(0);
          $table->primary(['path', 'method']);
      });
      
    • Add indexes for query performance:
      Schema::table('route_usage', function (Blueprint $table) {
          $table->index('last_used_at');
          $table->index('count');
      });
      
  4. Artisan Command:

    • Create a command to list used/dead routes:
      namespace App\Console\Commands;
      use Illuminate\Console\Command;
      use Illuminate\Support\Facades\DB;
      
      class ShowRouteUsage extends Command
      {
          protected $signature = 'route:usage {--dead|--used}';
          protected $description = 'List used or dead routes';
      
          public function handle()
          {
              $query = DB::table('route_usage')
                  ->orderBy('last_used_at', 'desc')
                  ->orderBy('path');
      
              if ($this->option('dead')) {
                  $query->whereNull('last_used_at');
              } elseif ($this->option('used')) {
                  $query->whereNotNull('last_used_at');
              }
      
              $routes = $query->get();
              $this->table(['Path', 'Method', 'Last Used', 'Count'], $routes->toArray());
          }
      }
      
  5. Optimization:

    • Batch Writes: Use Laravel’s queue system to defer database writes.
    • Cache Warmup: Exclude routes during cache warming (e.g., php artisan route:cache).
    • Exclusion List: Add middleware to skip tracking for specific routes (e.g., /health, /webhooks).

Compatibility

  • Laravel Versions: Tested on Laravel 8+ (due to middleware and Artisan command structures). May require adjustments for older versions.
  • Dependencies:
    • No hard Symfony dependencies if using middleware/model approach.
    • Ensure illuminate/database and illuminate/http are compatible with the target Laravel version.
  • Route Caching: Laravel’s route:cache may interfere with real-time tracking. Consider disabling caching during development or using a hybrid approach (e.g., track in dev, sample in production).

Sequencing

  1. Phase 1 (1-2 weeks):

    • Implement PoC middleware and database table.
    • Validate with a small set of routes.
  2. Phase 2 (1 week):

    • Develop Artisan command and exclusion logic.
    • Integrate with CI/CD for testing.
  3. Phase 3 (Ongoing):

    • Optimize performance (e.g., queued writes, read replicas).
    • Monitor false positives/negatives (e.g., API routes not being tracked).

Operational Impact

Maintenance

  • Custom Code Overhead:
    • The middleware, model, and command require ongoing maintenance (e.g., updates to Laravel’s routing system).
    • Unlike the Symfony package, there’s no upstream support; bugs or feature requests must be self-managed.
  • Dependency Management:
    • No external dependencies beyond Laravel core, reducing risk of breaking changes.
  • Documentation:
    • Internal docs must cover:
      • How to exclude routes from tracking.
      • Artisan
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.
daikazu/eloquent-salesforce-objects
unseen-codes/chat
romalytar/yammi-jobs-monitoring-laravel
kisame76/filament-db-table-state
nqxcode/laravel-lucene-search
dpfx/laravel-livewire-wizards
workos/workos-php-laravel
sofa/laravel-global-scope
nawasara/auth-primitives
adhocrat-io/arkhe-main
make-dev/orca-harpoon
itsemon245/lamet
baks-dev/dashboard
amoifr/pickle-panther-bundle
make-dev/orca
dmstr/symfony-system-resources-bundle
dmstr/symfony-job-queue-bundle
dmstr/openapi-json-schema-bundle
dmstr/keycloak-security-bundle
dmstr/doctrine-audit-log-bundle