I believe that many people are familiar with PHP trait, because Laravel can be seen everywhere,
But I still write it out and share it with those who are destined.
PHP trait allows two different CLASS to use the same method.
It not only reduces the complexity, but also allows the code to be reused.
So it should be very convenient to put a Browser series function on Laravel's ViewServiceProvider 😝
About ViewServiceProvider official documents:
https://laravel.com/docs/9.x/views#view-composers
It is said that Traits can be built and placed casually, so I made a Browser traits in Laravel's /app/Ccc/Traits directory.
Of course, the Namespace is the same as the directory rule, you should see it (the following code example)
Because of the large number of webp image files on this website, I Googled the function of getBrowser
(Sorry, I forgot the source when I found it casually), use it to detect the browser and version,
He had a bug where the $ub variable was undefined, and I fixed it later.
With this function, I can control whether to use webp image files (is_support_webp).
If you haven't read my webp conversion bash article (jpg to webp, and shrink jpg file), you can click the following article to see:
https://www.ccc.tc/article/make-the-web-faster
The other is to get another method, isMobile(). Used to detect whether it is a mobile phone.
The content of trait Browser is as follows: (simplified version)
namespace App\Ccc\Traits;
use Illuminate\Support\Facades\Log;
trait Browser
{
public function is_support_webp(){
if (($this->getBrowser()->name == "Google Chrome" && $this->getBrowser()->version >= 107) ||
($this->getBrowser()->name == "Apple Safari" && $this->getBrowser()->version >= 16) ||
($this->getBrowser()->name == "Mozilla Firefox" && $this->getBrowser()->version >= 65)
) {
return true;
} else {
return false;
}
}
public function isMobile() {
return preg_match("/(android|avantgo|blackberry|bolt|boost|cricket|docomo|fone|hiptop|mini|mobi|palm|phone|pie|tablet|up\.browser|up\.link|webos|wos) /i", $_SERVER["HTTP_USER_AGENT"]);
}
public function getBrowser()
{
$u_agent = $_SERVER['HTTP_USER_AGENT'];
$bname = 'Unknown';
$platform = 'Unknown';
$version = "";
$ub=""; //Default Value
//First get the platform?
if (preg_match('/linux/i', $u_agent)) {
$platform = 'linux';
} elseif (preg_match('/macintosh|mac os x/i', $u_agent)) {
$platform = 'mac';
} elseif (preg_match('/windows|win32/i', $u_agent)) {
$platform = 'windows';
}
// Next get the name of the useragent yes seperately and for good reason
if (preg_match('/MSIE/i', $u_agent) && !preg_match('/Opera/i', $u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
} elseif (preg_match('/Firefox/i', $u_agent)) {
$bname = 'Mozilla Firefox';
$ub = "Firefox";
} elseif (preg_match('/OPR/i', $u_agent)) {
$bname = 'Opera';
$ub = "Opera";
} elseif (preg_match('/Chrome/i', $u_agent) && !preg_match('/Edge/i', $u_agent)) {
$bname = 'Google Chrome';
$ub = "Chrome";
} elseif (preg_match('/Safari/i', $u_agent) && !preg_match('/Edge/i', $u_agent)) {
$bname = 'Apple Safari';
$ub = "Safari";
} elseif (preg_match('/Netscape/i', $u_agent)) {
$bname = 'Netscape';
$ub = "Netscape";
} elseif (preg_match('/Edge/i', $u_agent)) {
$bname = 'Edge';
$ub = "Edge";
} elseif (preg_match('/Trident/i', $u_agent)) {
$bname = 'Internet Explorer';
$ub = "MSIE";
}
// finally get the correct version number
$known = array('Version', $ub?? "0", 'other');
$pattern = '#(?' . join('|', $known) .
')[/ ]+(?[0-9.|a-zA-Z.]*)#';
if (!preg_match_all($pattern, $u_agent, $matches)) {
// we have no matching number just continue
}
// see how many we have
$i = count($matches['browser']);
if ($i != 1) {
//we will have two since we are not using 'other' argument yet
//see if version is before or after the name
if (strripos($u_agent, "Version") < strripos($u_agent, $ub)) {
$version = $matches['version'][0];
} else {
$version = $matches['version'][1];
}
} else {
$version = $matches['version'][0];
}
// check if we have a number
if ($version == null || $version == "") {
$version = "?";
}
return (object)[
'userAgent' => $u_agent,
'name' => $bname,
'version' => $version,
'platform' => $platform,
'pattern' => $pattern
];
}
}
With the above traits, I can know whether the user is using the mobile phone to open View or Desktop.
The following is also my simplified version 1. ViewServiceProvider uses App\Ccc\Traits\Browser.
use App\Ccc\Traits\Browser;
2. Pass the test result to $view
$view->with([
"is_mobile"=>$this->isMobile()
]);
Three, so the complete view of the ViewServiceProvider is roughly as follows.
Create this ViewServiceProvider file with the command:
php artisan make:provider ViewServiceProvider
In addition, register this Provider in the providers array in config/app.php
/*
* Application Service Providers...
*/
App\Providers\AppServiceProvider::class,
App\Providers\AuthServiceProvider::class,
// App\Providers\BroadcastServiceProvider::class,
App\Providers\EventServiceProvider::class,
App\Providers\RouteServiceProvider::class,
App\Providers\ViewServiceProvider::class,
namespace App\Providers;
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;
use Illuminate\Support\Facades\App;
use App\Ccc\Traits\Browser;
class ViewServiceProvider extends ServiceProvider
{
public $locale;
use Browser;
/**
* Register services.
*
* @return void
*/
public function register()
{
//
}
/**
* Bootstrap services.
*
* @return void
*/
public function boot()
{
View::composer('*', function ($view) {
$view->with([
"is_mobile"=>$this->isMobile()
]);
});
}
}
The above '*' should be guessed out, which means that all views do not need to be explained.
So any view, of course, includes my livewire components, and it is not a problem to use $is_mobile anywhere. It is very convenient to control whether it is for Mobile or Desktop.
For example: In the Livewire component below, I asked him to use a small phone-specific page for the mobile phone page.
{{-- Display the mobile version dedicated page --}}
@if ($is_mobile)
{{ $contents->links('livewire.mobile-pages') }}
@endif
@foreach ($contents as $content)
@if ($loop->first&&!$is_mobile)
Similar Stories
Laravel linux
My mobile computer, install Laravel 8 on Sony xperia 5 II
This is my first Android phone. It's no problem to try to install Laravel. Basically, I installed vim and mariadb. After executing sshd, you can log in through 8022, it is completely a mobile computer: p. The software I use is Termux...
laravel
Laravel 5.6 has those new changes
Laravel 5.6 is scheduled to be released in February 2018, what changes? Let's see. (Argon2i Password Hashing Algorithm) Argon2 provides the following three versions: 1. In Laravel 5.6, Argon2i password hashing algorithm will be supported. (Argon2i Password Hashing Algorithm) 2. Argon2d resists GPU cracking attacks to the greatest extent. 3. Argon2i is optimized to resist side-channel attacks. Third, Argon2id is a hybrid version. It follows the Argon2i method for the first pass, and then uses the Argon2d method for subsequent passes. It doesn't matter if you don't understand it, I don't understand it anyway, the point is, it's safe to be sure anyway.This is the Open Cryptography Contest (PHC) on July 20, 2015
laravel
Facebook test user
Is there anyone who uses OAuth for website user login like me? There is a "test user" on the Facebook developer page. It can be used to test whether the function of Facebook is normal, Because when the website moved to Google’s GCE, for some reason, I actually posted one more in the $fillable array of Laravel’s User model~, Normally the program works fine, but when a new user logs in, an error is shown to you. I keep hearing people say that I can't log in to this website to leave a message. I want to say that I am very normal. @@ After using the test user test today, ha, I found that the login function on my website has been broken for a long time.
No Comment
Post your comment