Do I Really Need This Route Anymore?

Posted: 2015-12-17 16:34:45

Clean out old code can be hard. When your application has numerous javascript widgets, blade templates and forms you can not easily say what routes are being used. But with just a few artisan commands we can easily make this happen.

Also this can help with knowing what routes your Tests are NOT covering.

Package can be found here as well.

route report

The Commands to Get Started

php artisan make:model RouteUsage
php artisan make:middleware RouteUsageTracker
php artisan make:migration route_usage_tracker_table
php artisan make:console RouteReport

Migration

<?php

use Illuminate\Database\Schema\Blueprint;
use Illuminate\Database\Migrations\Migration;

class RouteUsageTrackerTable extends Migration
{
    public function up()
    {
        Schema::create('route_usages', function (Blueprint $table) {
                $table->increments('id');

                $table->string('path');

                $table->timestamps();
            });
    }

    /**
     * Reverse the migrations.
     *
     * @return void
     */
    public function down()
    {
        Schema::drop('route_usages');
    }
}

Middleware

<?php

namespace App\Http\Middleware;

use App\RouteUsage;
use Closure;

class RouteUsageTracker
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure  $next
     * @return mixed
     */
    public function handle($request, Closure $next)
    {
        RouteUsage::create(['path' => $request->path()]);

        return $next($request);
    }
}

And plug that into the Kernel.php


class Kernel extends HttpKernel { /** * The application's global HTTP middleware stack. * * @var array */ protected $middleware = [ \Illuminate\Foundation\Http\Middleware\CheckForMaintenanceMode::class, \App\Http\Middleware\EncryptCookies::class, \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class, \Illuminate\Session\Middleware\StartSession::class, \App\Http\Middleware\RouteUsageTracker::class, ];

Model

<?php

namespace App;

use Illuminate\Database\Eloquent\Model;

class RouteUsage extends Model
{
    protected $fillable = ['path'];
}

Artisan Reporting

This can use more work like pagination, filtering etc. I used the RouteListCommand to get started

<?php

namespace App\Console\Commands;

use App\RouteUsage;
use Illuminate\Console\Command;
use Illuminate\Support\Facades\DB;

class RouteReport extends Command
{
    /**
     * The name and signature of the console command.
     *
     * @var string
     */
    protected $signature = 'route:report {page?} {--count} ';

    protected $headers          = ['Path', 'Date'];
    protected $headers_count    = ['count', 'Path'];
    /**
     * The console command description.
     *
     * @var string
     */
    protected $description = 'Command description';

    /**
     * Create a new command instance.
     *
     * @return void
     */
    public function __construct()
    {
        parent::__construct();
    }

    /**
     * Execute the console command.
     *
     * @return mixed
     */
    public function handle()
    {

        if($this->option('count'))
        {
            $report = RouteUsage::select(DB::raw('count(*) as used, path'))->groupBy('path')->orderBy('used', 'DESC')->get()->toArray();

            $this->table($this->headers_count, $report);
        }
        else
        {
            $report = RouteUsage::select('path', 'created_at')->paginate(100);

            $this->table($this->headers, $report->toArray()['data']);
        }

    }
}

php artisan route:report --count

Can give us

counted


Tags:

php