spinen / laravel-browser-filter
Filters http requests based on browser type.
Installs: 42 915
Dependents: 0
Suggesters: 0
Security: 0
Stars: 20
Watchers: 9
Forks: 3
Open Issues: 2
Requires
- php: >=8.1
- ext-json: *
- illuminate/cache: ^9.19|^10|^11
- illuminate/routing: ^9.19|^10|^11
- illuminate/support: ^9.42|^10|^11
- mobiledetect/mobiledetectlib: ~2.8
- ua-parser/uap-php: ~3.9
Requires (Dev)
- illuminate/http: ^9.19|^10
- laravel/pint: ^1.2
- mockery/mockery: ^1.5.1
- phpunit/phpunit: ^9.6.5
- psy/psysh: ^0.11
- symfony/var-dumper: ^6.2
- dev-develop
- 3.2.0
- 3.1.0
- 3.0.0
- 2.3.2
- 2.3.1
- 2.3.0
- 2.2.0
- 2.1.0
- 2.0.1
- 2.0.0
- 1.1.0
- 1.0.1
- 1.0.0
- 0.8.0
- 0.7.0
- 0.6.0
- 0.5.0
- 0.4.0
- 0.3.6
- 0.3.5
- 0.3.4
- 0.3.3
- 0.3.2
- 0.3.1
- 0.3.0
- 0.2.1
- 0.2.0
- 0.1.0
- dev-master
- dev-feature/supportL10
- dev-feature/supportLaravel9
- dev-thaisonle-patch-1
- dev-feature/supportLaravel6
- dev-feature/cleanupRepo
This package is auto-updated.
Last update: 2024-11-08 13:21:47 UTC
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
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
.