The jenssegers-agent package deal is a desktop/cell person agent parser with assist for Laravel, primarily based on MobileDetect. You should use this package deal in any PHP software, and it additionally supplies a Laravel service supplier, providing you with a service Facade:
use JenssegersAgentFacadesAgent;
Agent::is('Firefox');
Agent::is('iPhone');
// Magic strategies
Agent::isFirefox();
Agent::isIPhone();
// System kind
Agent::isDesktop();
Agent::isMobile();
Agent::isTablet();
Agent::isPhone();
Moreover person agent helpers, the Agent service supplies a language helper, system identify methodology, platform, and extra. For instance, you possibly can get the accepted browser languages utilizing the next methodology:
Agent::languages(); // ['en-us', 'en']
Utilizing the languages methodology, you possibly can set the locale throughout a request in a middleware. This is a easy instance simply for example simply off the highest of my head:
public perform deal with(Request $request, Closure $subsequent): Response
{
$supported_locales = ['en', 'es'];
$user_locales = Agent::languages();
foreach ($user_locales as $locale) {
if (in_array($locale, $supported_locales)) {
app()->setLocale($locale);
}
}
return $subsequent($request);
}
If you don’t assist the locale, the config('app.fallback_locale') setting will outline the locale. You may also use the languages() methodology in middleware to redirect a locale-specific route prefix /{locale}/ primarily based on the person agent’s supported language.
Route::prefix('/{locale}')->group(perform () {
// ...
})->whereIn('locale', ['en', 'es']);
The Agent service may decide if the present person agent is a bot and what kind of bot:
// Is the person a bot?
Agent::isRobot(); // bool
// get the robotic identify
Agent::robotic();
Lastly, to get the system identify, platform, and browser, you should utilize the aptly named strategies on the facade:
Agent::system(); // "Macintosh"
Agent::platform(); // "OS X"
Agent::browser(); // "Safari"

