Roots Discourse

Browser Detection Laravel extension for Sage

I have found this browser detection library Laravel/Blade.

I have composer installed it in the sage theme directory but don’t know how to then integrate it so I can use the blade languages for it in templates?

Anyone know how it could be done?

I have tried adding this to my app/App.php

use Browser;

but no joy?

What is the error? class not found?

My two cents here: I would use a multilanguage plugin like Polylang and then use the WordPress locale in the theme:

After a few bits and bats I have managed to get it working.

After the composer install just adding this to app/controllers/App.php

<?php

namespace App\Controllers;

use hisorange\BrowserDetect\Parser as Browser;
use Sober\Controller\Controller;

class App extends Controller
{
    public function browser()
    {
        return new Browser;
    }

    // other stuff...
}

This then allows me to call the libraries functions in my blade templates like so…

@if($browser::isDesktop())
    <p>This is a desktop</p>
@endif
1 Like

@benword Do you think this browser detection library could be included as part of the Sage core so we could use the blade syntax in the layouts/partials?

My two cents here: Feature detection over browser detection (https://developer.mozilla.org/en-US/docs/Learn/Tools_and_testing/Cross_browser_testing/Feature_detection)
Using CSS @supports and JavaScript over server-side solutions.

Yeah I totally get that client-side browser detection is a solid way to go about this in general but what I was more interested in was with how library detects devices:

Browser::isMobile();
Browser::isTablet();
Browser::isDesktop();

and its use in blade templates

@mobile
    <p>This is the MOBILE template!</p>
    @include('your-mobile-template')
@endmobile

@tablet
    <p>This is the TABLET template!</p>
    <link rel="stylesheet" href="tablet.css" title="Reduce the page size, load what the user need">
@endtablet

@desktop
    <p>This is the DESKTOP template!</p>
@enddesktop

The blade stuff is only useable in a full Laravel framework but for me and other developers I know this could cut down on quite a lot the output of HTML, CSS and JS when building fully responsive websites. My thought being that server-side detection would be faster and wouldn’t need JS or CSS to be loaded before detection occurs?

Admittedly I’m only testing out this library for fun at the moment but if it proves to be of real use in a production environment, I was just curious as to its potential in Sages core.

I doubt this would be included in Sage core. If you want to use it, Sage 10 is more deeply integrated with Laravel tools and might support it.