dqudtskm49788
2016-08-11 01:33 阅读 111
已采纳

如何访问Tracker(antonioribeiro / tracker)对象在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?

  • 点赞
  • 写回答
  • 关注问题
  • 收藏
  • 复制链接分享

2条回答 默认 最新

  • 已采纳
    douan6815 douan6815 2016-08-11 02:02

    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.

    点赞 评论 复制链接分享
  • douyanlu7380 douyanlu7380 2016-08-11 02:34

    The answer to this question provides a solution for (2), I found the answer to (1) here.

    To access the tracker object you need to include:

    use PragmaRX\Tracker\Vendor\Laravel\Facade as tracker;
    

    Then you'll do something like:

    $visitor = Tracker::currentSession();
    

    as described in the documentation.

    点赞 评论 复制链接分享

相关推荐