Artur Grigio Artur Grigio - 1 year ago 100
PHP Question

How to access Tracker (antonioribeiro/tracker) object In Laravel

I am using AntonioRiveiro/Tracker repo for my laravel app. I would like to add some more functionality to it that it doesn't have out of the box.

  1. But I can't find where its instance gets created, or how to access the
    object in my Controllers.

  2. Or how do I register a custom hit to one of my views?

Answer Source

So I think the easiest way for you to go about this is the following:

First, make your own ServiceProvider

php artisan make:provider CustomTrackerServiceProvider

Now open up that file, we'll need to make some modifications.

Firstly, we'll need to extend the ServiceProvider that Tracker provides.

use PragmaRX\Tracker\Vendor\Laravel\ServiceProvider as TrackerServiceProvider

class CustomTrackerServiceProvider extends TrackerServiceProvider

The use alias above is not required, but I prefer it for clarity given the similar naming convention to the core framework.

Now you'll need to replace your usage of the PragmaRX\Tracker\Vendor\Laravel\ServiceProvider in your config/app.php file under the providers array

'providers' => [
    //other providers and what have you

Now you have the ability to make changes. You can override the default functionality of the core class as long as it's member is not private.

Have a look at the vendor provided file and identify a similar architecture pattern to the vendor maintainer, and you'll be good to go.