vxm / laravel-mobile-first
支持为您的Laravel应用程序实现移动优先原则。
Requires
- php: ^7.1
- illuminate/support: ^5.7
- jenssegers/agent: ^2.6
Requires (Dev)
- orchestra/testbench: ^3.7
- phpunit/phpunit: ~7.5
- scrutinizer/ocular: ^1.5
Suggests
- riverskies/laravel-mobile-detect: For detect device in blade environment.
This package is auto-updated.
Last update: 2024-08-27 04:14:39 UTC
README
Laravel Mobile First
关于它
这是一个基于Jenssegers Agent的包,支持您实现基于移动优先原则。
安装
使用Composer安装Laravel Mobile First
composer require vxm/laravel-mobile-first
安装后,您需要使用以下命令发布配置文件:
php artisan vendor:publish --provider="VXM\MobileFirst\MobileFirstServiceProvider" --tag="config"
这是发布的配置文件内容
return [ /** * Your mobile site use to redirect when user not using desktop device. * Note: it only affect when you registered `VXM\MobileFirst\MobileRedirect` middleware. */ 'mobile_url' => 'https://m.yoursite.com', /** * Keep url path when redirect to mobile url (ex: https://yoursite.com/abc to https://m.yoursite.com/abc). */ 'keep_url_path' => true, /** * HTTP status code will be set when redirect to mobile url. */ 'redirect_status_code' => 301, /** * HTTP request method should be redirect to mobile url. */ 'redirect_methods' => ['OPTIONS', 'GET'], /** * Enable auto switch view by device type. * When enabled, the system auto switch view to compatible view (sub-view) by user device type (ex: 'index.blade.php' => 'mobile/index.blade.php'), * compatible view will be find on `device_sub_dirs`. If not found, not affect. */ 'auto_switch_view_by_device' => false, /** * An array with key is device type and value is sub dir of it. Use to switch view to compatible view (sub-view) by user device type. */ 'device_sub_dirs' => [ //'phone' => 'phone', // switch when device is phone. //'tablet' => 'tablet', // switch when device is tablet. //'android' => 'android', // switch when device os is android. //'ios' => 'ios', // switch when device os is ios. 'mobile' => 'mobile', // switch when device is tablet or phone. ], ];
用法
此包为您提供了两个功能
移动重定向
您需要在您的mobilefirst
配置文件中设置您的移动站点URL
'mobile_url' => 'https://m.yoursite.com',
现在添加中间件
// app/Http/Kernel.php protected $middleware = [ ... \VXM\MobileFirst\MobileRedirect::class, ],
自动切换视图
默认情况下,此功能是禁用的,您需要在mobilefirst
配置文件中启用它
'auto_switch_view_by_device' => true,
这是一种在不修改原始视图渲染代码的情况下通过用户设备替换一组视图的方法。您可以使用它来根据用户设备系统性地更改应用程序的外观和感觉。例如,当调用view('about')
时,您将渲染视图文件resources/views/about.blade.php
,如果用户使用移动设备,则将渲染视图文件resources/views/mobile/about.blade.php
,而不是。
device_sub_dirs
控制视图文件应如何根据用户设备替换。它接受一个键值对数组,其中键是设备类型,值是对应的视图子目录。替换基于用户设备:如果用户设备与device_sub_dirs
数组中的任何键匹配,则将添加具有相应子目录值的视图路径。使用上述配置示例,当用户使用移动设备(因为它匹配键mobile)时,将添加一个看起来像resources/views/mobile/about.blade.php
的视图路径。当然,您可以更改值或添加更多情况。
'device_sub_dirs' => [ 'ios' => 'apple', // switch when device os is ios. 'mobile' => 'mobile', // switch when device is tablet or phone. ],
如果用户使用iOS,视图路径将添加apple
。