Search code examples
phplaravelobjecttrackingtracker

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 Tracker object in my Controllers.

  2. Or how do I extend the tracker object?


Solution

  • 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

    config/app.php
    'providers' => [
        //other providers and what have you
        App\Http\Providers\CustomTrackerServiceProvider::class
    ]
    

    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.