您是否曾為跨不同裝置的使用者提供正確的體驗而苦苦掙扎?在用戶在手機、平板電腦和桌上型電腦之間無縫切換的時代,準確檢測設備可能是一個令人頭痛的問題。這就是 EprofosUserAgentAnalyzerBundle 的用武之地。
與傳統的使用者代理程式解析器不同,EprofosUserAgentAnalyzerBundle 提供:
composer require eprofos/user-agent-analyzer
就是這樣!該捆綁包會在您的 Symfony 應用程式中自動配置自身。
改造這個複雜的檢測:
if (strpos($_SERVER['HTTP_USER_AGENT'], 'Mobile') !== false) { // Is it really mobile? What about tablets? // What about desktop mode on mobile? }
進入這個優雅的解決方案:
{% if is_mobile() %} <div class="mobile-view"> {{ include('components/mobile_navigation.html.twig') }} </div> {% endif %}
use Eprofos\UserAgentAnalyzerBundle\Service\UserAgentAnalyzer; class ResponsiveController { public function index(UserAgentAnalyzer $analyzer) { $result = $analyzer->analyzeCurrentRequest(); // Rich device information $deviceInfo = [ 'type' => $result->getDeviceType(), 'os' => [ 'name' => $result->getOsName(), 'version' => $result->getOsVersion(), 'is64bit' => $result->is64BitsMode() ], 'browser' => [ 'name' => $result->getBrowserName(), 'version' => $result->getBrowserVersion(), 'isWebview' => $result->isBrowserAndroidWebview() || $result->isBrowserIosWebview() ] ]; // Use this information to: // 1. Optimize content delivery // 2. Enable platform-specific features // 3. Track usage analytics } }
{# Optimize product gallery based on device #} {% if is_mobile() %} {# Show swipeable gallery #} {{ include('product/mobile_gallery.html.twig') }} {% elseif is_tablet() %} {# Show touch-optimized grid #} {{ include('product/tablet_gallery.html.twig') }} {% else %} {# Show full desktop experience #} {{ include('product/desktop_gallery.html.twig') }} {% endif %}
{# Enable platform-specific features #} {% if is_android() %} {# Android-specific PWA features #} {{ include('pwa/android_install_prompt.html.twig') }} {% elseif is_ios() %} {# iOS-specific PWA features #} {{ include('pwa/ios_install_prompt.html.twig') }} {% endif %}
use Eprofos\UserAgentAnalyzerBundle\Service\UserAgentAnalyzer; class MediaController { public function serveVideo(UserAgentAnalyzer $analyzer) { $result = $analyzer->analyzeCurrentRequest(); // Optimize video delivery $videoConfig = match(true) { $result->isMobile() => [ 'quality' => 'adaptive', 'preload' => 'metadata', 'format' => 'mp4' ], $result->isTablet() => [ 'quality' => 'high', 'preload' => 'auto', 'format' => 'mp4' ], default => [ 'quality' => 'highest', 'preload' => 'auto', 'format' => 'webm' ] }; return $this->render('video/player.html.twig', [ 'config' => $videoConfig ]); } }
$result = $analyzer->analyzeCurrentRequest(); // Check for specific browser features if ($result->getBrowserChromiumVersion()) { // Enable Chrome-specific features } if ($result->getBrowserWebkitVersion()) { // Enable WebKit-specific features } // Check for desktop mode on mobile if ($result->isBrowserDesktopMode()) { // Adjust layout accordingly }
// Detailed OS information $osInfo = match($result->getOsName()) { 'Windows' => [ 'version' => $result->getOsVersion(), 'family' => $result->getOsFamily(), 'is64bit' => $result->is64BitsMode() ], 'macOS' => [ 'version' => $result->getOsVersion(), 'codename' => $result->getOsCodename(), // e.g., "Monterey" 'architecture' => $result->is64BitsMode() ? 'x64' : 'x86' ], default => [ 'name' => $result->getOsName(), 'version' => $result->getOsVersion() ] };
此捆綁包的設計考慮了性能:
EPROFOS(École professionalnelle de Formation spécialisée)是網路、行動和軟體開發專業培訓的參考學校。我們專注於開發高品質的 Web 解決方案,並致力於創建幫助開發者更有效率地建立更好的應用程式的工具。
composer require eprofos/user-agent-analyzer
存取我們的 GitHub 儲存庫:
加入我們,讓 Web 開發更有智慧、更有效率!
以上是透過智慧型裝置偵測增強您的 Symfony 應用程式:認識 EprofosUserAgentAnalyzerBundle的詳細內容。更多資訊請關注PHP中文網其他相關文章!