Route Usage for Laravel

License MIT Latest version Route Usage for Laravel Build status of Route Usage for Laravel

This package keeps track of all requests in order to know what controller method was used. The goal is to find out if there are unused endpoints or controller methods in your app. This is not to build any sort of analytics!

After a few years, any projects have dead code and unused endpoints. Typically, you removed a link on your frontend, nothing ever links to that old /special-page. You might want to remove it, but you're not sure.

With this package, you can have look at the route_usage table and figure out when this page was accessed for the last time.

When was this page used for the last time? Last week? Better keep it for now. 3 years ago? REMOVE THE CODE! 🥳

Notes

  • This package only logs request with a 2xx or 3xx HTTP response. I don't think the rest make sense but if you think it does, please reach out.
  • In the very first version, I was incrementing a count attribute. I removed it because I think it gives a wrong information. If it was used a lot but wasn't accessed for over a year, it might give a false sense of importance to this unused route.

Installation

Install the package via composer.

composer require julienbourdeau/route-usage

Run migrations to create the new route_usage table.

php artisan migrate

Optionally, publish the configuration if you need to modify it.

php artisan vendor:publish --provider="Julienbourdeau\RouteUsage\RouteUsageServiceProvider" --tag=config

Usage

Command line

To access the route usage, you can do it in your terminal with the command.

php artisan usage:route

HTML table

To access the HTML table, you'll first need to define who can access it. By default, it's available only on local environment.

In your AuthServiceProvide, in the boot method, specify who can access this page with a gate.

Gate::define('viewRouteUsage', function ($user) {
    return $user->isSuperAdmin();
});

Then, head over to yourapp.tld/route-usage.

Configuration

excluding-regex

Here you may specify regex to exclude routes from being logged. Typically, you want may want to exclude routes from packages or dev controllers. The value must be a valid regex or anything falsy.

Road to 1.0 release

Less SQL queries

I'd like to reduce the number of SQL queries performed. Typically, if a route is called, we don't need to log usage for the next 5 minutes. I'm thinking we could use some cache or split the data: attributes in mysql but last used date in Redis.

💡 Feel free to open an issue to discuss your ideas.

More package to ignore

Today, we ignore routes from Nova or Debugbar because there is nothing you can do about these routes. I'd like to support more packages out of the box.

📦 What package would you like to see added?

Changelog

v0.4

07 Dec 2019 | git diff 0.3..0.4

  • Support other databases - PR #5

    Instead of a raw MySQL query, the package is now using Eloquent to save the route usage entries. This brings support to other databases, so tests are now running using SQLite.

v0.3

29 Oct 2019 | git diff 0.2..0.3

  • Hide HTML page behind Gate - PR #13

    In your AuthServicePrivder::boot, define a gate:

    Gate::define('viewRouteUsage', function ($user) {
        return $user->isSuperAdmin();
    });
    

v0.2

28 Oct 2019 | git diff 0.1..0.2

  • Ignore OPTIONS http calls - PR #12

  • Ignore routes based on their name or uri - PR #6

  • Add style for HTML view - PR #8

v0.1

  • Initial release