spinen/laravel-browser-filter
Composer 安装命令:
composer require spinen/laravel-browser-filter
包简介
Filters http requests based on browser type.
README 文档
README
This is a Laravel 5 middleware to filter routes based on browser types.
We specify the browsers that we are going to support at the beginning of a project, so this package makes sure that the visitor is using a supported browser.
Build Status
| Branch | Status | Coverage | Code Quality |
|---|---|---|---|
| Develop | |||
| Master |
Prerequisites
NOTE: If you need to use PHP <7.2 or Laravel <5.2, please stay with version 1.x
As side from Laravel >= 5.5, there are 2 packages that are required:
- mobiledetect - To get the user agent string. This package is not needed to get to the user agent string, but there are other features that I plan on using in the future so I kept it installed.
- ua-parser PHP Library - To parse the user agent string
Install
Install Browser Filter:
$ composer require spinen/laravel-browser-filter
The package uses the auto registration feature of Laravel 5.
'providers' => [ // ... Spinen\BrowserFilter\FilterServiceProvider::class, ];
Register the middleware
The middleware needs to be registered with the Kernel to allow it to parse the request.
Laravel 11 & newer
Register the HTTP Stack Middleware for the web group in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { // ... $middleware->web(append: [ // ... \Spinen\BrowserFilter\Stack\Filter::class, ]); // ... })
Register the Route Middlewares in bootstrap/app.php:
->withMiddleware(function (Middleware $middleware) { // ... $middleware->alias([ // ... 'browser.allow' => \Spinen\BrowserFilter\Route\AllowFilter::class, 'browser.block' => \Spinen\BrowserFilter\Route\BlockFilter::class, ]); // ... })
Before Laravel 11
Register the HTTP Stack Middleware for the web group in app/Http/Kernel.php:
protected $middlewareGroups = [ 'web' => [ // .. \Spinen\BrowserFilter\Stack\Filter::class, ], // ..
Register the Route Middlewares in app/Http/Kernel.php:
protected $routeMiddleware = [ // .. 'browser.allow' => \Spinen\BrowserFilter\Route\AllowFilter::class, 'browser.block' => \Spinen\BrowserFilter\Route\BlockFilter::class,
Page to show if blocked
Build a page with a named route to redirect blocked browsers to:
// This is only a simple example. You would probably want to route to a controller with a view. Route::get('incompatible_browser', ['as' => 'incompatible_browser', 'uses' => function() { return "You are using a blocked browser."; }]);
Configure middleware options
Publish the package config file to config/browserfilter.php:
$ php artisan vendor:publish --provider="Spinen\BrowserFilter\FilterServiceProvider"
This file is fully documented, so please read it to know how to configure the middleware. There are 4 top level items that you can configure...
- type - The type of filtering strategy to apply to the stack filter
- rules - The array of devices/browsers/versions to allow or block for ALL http requests
- route - The name of the route to redirect the user to if they are using a blocked client
- timeout - The length of time to cache the client data, where "0" disables the cache
Using the Route middleware
The route middleware uses the same configuration file as the stack middleware, but ignores the rules.
The rules are passed in after the ':' behind the route filter that you wish to use...
Route::get('tablet_page', [ 'middleware' => 'browser.allow:Tablet', 'uses' => function () { return "Special page that is only accessible to tablets"; } ]);
or
Route::get('ie_is_blocked_page', [ 'middleware' => 'browser.block:Other/Ie', 'uses' => function () { return "Special page that is only accessible to non IE browsers on Desktops"; } ]);
The format of the filter is Device/Browser/operatorVersion|operatorVersion2;Device/Browser2/operatorVersion, so the following rule:
$rule = [ 'Mobile' => '*', 'Other' => [ 'Ie' => [ '<' => '10', '>' => '13', ], ], 'Tablet' => '*', ]
would be written as: Mobile;Other/Ie/<10|>13;Tablet.
spinen/laravel-browser-filter 适用场景与选型建议
spinen/laravel-browser-filter 是一款 基于 PHP 开发的 Composer 扩展包,目前已累计 47.17k 次下载、GitHub Stars 达 20, 最近一次更新时间为 2015 年 11 月 10 日, 在 PHP 生态内属于活跃度较高的组件。
它主要适用于以下技术方向: 「middleware」 「laravel」 「spinen」 「browser hawk」 等业务场景。在实际项目中,围绕这些方向常见需要落地的问题包括:接口对接、性能调优、并发安全、与既有框架(Laravel / ThinkPHP / Yii / Webman 等)的兼容适配,以及生产环境的日志埋点与稳定性保障。
我们在过去多个企业项目中使用过 spinen/laravel-browser-filter 或与其功能相近的方案,如果你在选型或落地过程中遇到问题,例如 版本兼容、二次改造、私有化封装、与内部系统对接、生产 BUG 排查,欢迎联系我们协助评估。
基于 spinen/laravel-browser-filter 在你已有业务上做功能扩展、字段裁剪、UI 适配、与内部账号 / 权限 / 日志系统的深度对接。
线上偶发问题、内存泄漏、慢查询、并发异常等排查修复;针对高流量场景做缓存、队列、索引层面的调优。
承接完整的项目从需求 → 设计 → 开发 → 上线 → 长期运维;也可按月提供技术保姆服务。
与 spinen/laravel-browser-filter 相关的其它包
同方向 / 同关键字的高下载量 PHP Composer 包推荐,方便对比选型:
Shoot aims to make providing data to your templates more manageable
Helpers to keep track of the version of your Laravel application.
SPINEN's Laravel Package for ClickUp.
Slim Framework 3 CSRF protection middleware utilities
Cloudflare Middleware For Guzzle
PSR-15 middleware to handle CSRF-token verification
统计信息
- 总下载量: 47.17k
- 月度下载量: 0
- 日度下载量: 0
- 收藏数: 21
- 点击次数: 3
- 依赖项目数: 0
- 推荐数: 0
其他信息
- 授权协议: MIT
- 更新时间: 2015-11-10