spatie/laravel-referer
Composer 安装命令:
composer require spatie/laravel-referer
包简介
Keep a visitor's original referer in session
关键字:
README 文档
README
Remember a visitor's original referer in session. The referer is (highest priority first):
- The
utm_sourcequery parameter - The domain from the request's
Refererheader if there's an external host in the URL - Empty
Support us
We invest a lot of resources into creating best in class open source packages. You can support us by buying one of our paid products.
We highly appreciate you sending us a postcard from your hometown, mentioning which of our package(s) you are using. You'll find our address on our contact page. We publish all received postcards on our virtual postcard wall.
Installation
You can install the package via composer:
composer require spatie/laravel-referer
The package will automatically register itself in Laravel 5.5. In Laravel 5.4. you'll manually need to register the Spatie\Referer\RefererServiceProvider service provider in config/app.php.
You can publish the config file with:
php artisan vendor:publish --provider="Spatie\Referer\RefererServiceProvider"
Publishing the config file is necessary if you want to change the key in which the referer is stored in the session or if you want to disable a referer source.
return [ /* * The key that will be used to remember the referer in the session. */ 'session_key' => 'referer', /* * The sources used to determine the referer. */ 'sources' => [ Spatie\Referer\Sources\UtmSource::class, Spatie\Referer\Sources\RequestHeader::class, ], ];
Usage
To capture the referer, all you need to do is add the Spatie\Referer\CaptureReferer middleware to your middleware stack. In most configuration's, you'll only want to capture the referer in "web" requests, so it makes sense to register it in the web stack. Make sure it comes after Laravel's StartSession middleware!
// app/Http/Kernel.php protected $middlewareGroups = [ 'web' => [ // ... \Illuminate\Session\Middleware\StartSession::class, // ... \Spatie\Referer\CaptureReferer::class, // ... ], // ... ];
The easiest way to retrieve the referer is by just resolving it out of the container:
use Spatie\Referer\Referer; $referer = app(Referer::class)->get(); // 'google.com'
Or you could opt to use Laravel's automatic facades:
use Facades\Spatie\Referer\Referer; $referer = Referer::get(); // 'google.com'
The captured referer is (from high to low priority):
- The
utm_sourcequery parameter, or: - The domain from the request's
Refererheader if there's an external host in the URL, or: - Empty
An empty referer will never overwrite an exisiting referer. So if a visitor comes from google.com and visits a few pages on your site, those pages won't affect the referer since local hosts are ignored.
Forgetting or manually setting the referer
The Referer class provides dedicated methods to forget, or manually set the referer.
use Facades\Spatie\Referer\Referer; Referer::put('google.com'); Referer::get(); // 'google.com' Referer::forget(); Referer::get(); // ''
Changing the way the referer is determined
The referer is determined by doing checks on various sources, which are defined in the configuration.
return [ // ... 'sources' => [ Spatie\Referer\Sources\UtmSource::class, Spatie\Referer\Sources\RequestHeader::class, ], ];
A source implements the Source interface, and requires one method, getReferer. If a source is able to determine a referer, other sources will be ignored. In other words, the sources array is ordered by priority.
In the next example, we'll add a source that can use a ?ref query parameter to determine the referer. Additionally, we'll ignore ?utm_source parameters.
First, create the source implementations:
namespace App\Referer; use Illuminate\Http\Request; use Spatie\Referer\Source; class RefParameter implements Source { public function getReferer(Request $request): string { return $request->get('ref', ''); } }
Then register your source in the sources array. We'll also disable the utm_source while we're at it.
return [ // ... 'sources' => [ App\Referer\RefParameter::class, Spatie\Referer\Sources\RequestHeader::class, ], ];
That's it! Source implementations can be this simple, or more advanced if necessary.
Changelog
Please see CHANGELOG for more information what has changed recently.
Testing
composer test
Contributing
Please see CONTRIBUTING for details.
Security
If you've found a bug regarding security please mail security@spatie.be instead of using the issue tracker.
Credits
License
The MIT License (MIT). Please see License File for more information.
spatie/laravel-referer 适用场景与选型建议
spatie/laravel-referer 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 862.77k 次下载、GitHub Stars 达 527, 最近一次更新时间为 2017 年 02 月 02 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「spatie」 「laravel-referer」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spatie/laravel-referer 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spatie/laravel-referer 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spatie/laravel-referer 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Receive webhooks in Laravel apps
Easily optimize images using PHP
Store your application settings
Add tags and taggable behaviour to your Laravel app
Speed up a Laravel application by caching the entire response additions
Manage events on a Google Calendar
统计信息
- 总下载量: 862.77k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 532
- 点击次数: 28
- 依赖项目数: 6
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2017-02-02