Laravel, do not track routes with `antonioribeiro/

2019-09-03 01:31发布

问题:

I'm currently using antonioribeiro/tracker for some stats on my Laravel.

I'm struggling using the do_not_track_routes array in the config.

I just try to disable the tracker to track admin route, but it doesn't work.

    'do_not_track_routes' =>
    [
    'admin.*',
    ]

    /* Admin routes */
    Route::group(['namespace' => 'Admin', 'as'=>'admin', 'prefix' => 'admin1452872135', 'middleware' => ['auth', 'admin', 'language']], function () {
        //some route
    });

Even this keep the tracker to insert new sessions...

    'do_not_track_routes' =>
    [
    '*',
    ]

I really try everything that came to my mind, I'll be thankfull for some help...

回答1:

Get my own answer after debugging inside the package ... In fact, do_not_track_routes is never used in the Tracker.php (vendor\pragmarx\tracker\src\Tracker.php)

So I update the isTrackable method like that :

protected function isTrackable() {
    return $this->config->get('enabled') &&
    $this->logIsEnabled() &&
    $this->parserIsAvailable() &&
    $this->isTrackableIp() &&
    $this->isTrackableEnvironment() &&
    $this->notRobot() &&
    $this->isTrackableRoute();
}

And add this new method, to check if the current URL is in the do_not_track_routearray :

protected function isTrackableRoute() {
    if (is_null(\Request::path())) {
            return true;
    }
    $routes = $this->config->get('do_not_track_routes');
    foreach ($routes as $route) {
            $match = preg_grep ('/'. $route .'/i', [\Request::path()]);
            if(!empty($match)){
                return false;
            }
    }
    return true;
}

Work well for me, if you find a better answer, I'm in ! :)



回答2:

You must indicate your route's name (not the URI) under do_not_track_routes.

So if your route is defined as 'as'=>'admin' your tracker config must be

'do_not_track_routes' =>
[
    'admin',
]

In response to @Jiedara, do_not_track_routes is used in PragmaRX\Tracker\Data\Repositories\Route which checks if it includes $route->currentRouteName(). $route is an instance of Illuminate\Routing\Router and the currentRouteName() method indeed provides the route's name.