我已经阅读了如何为视图添加不同的路径或名称空间,但我认为这对我来说不是一个合适的解决方案。 我想做的是为移动设备设置一个视图基本路径,为桌面设备设置一个不同的基本路径,因此在视图控制器中我不需要进行任何更改。
在路由文件中设置路径并且不要触摸任何视图控制器会很棒。有什么想法吗?也许只是 Config::设置视图路径?
提前致谢! :)
对于未来正在寻找在视图中检测设备的方法的 Laravel 用户;另一种选择是创建一个 ServiceProvider - 然后使用
View::share()
- 这将使设备检测 $agent
在您的所有视图中可用。
composer require jenssegers/agent
php artisan make:provider AgentServiceProvider
App\Providers\AgentServiceProvider::class,
<?php
namespace App\Providers;
use View;
use Jenssegers\Agent\Agent;
use Illuminate\Support\ServiceProvider;
class AgentServiceProvider extends ServiceProvider
{
public function boot()
{
$agent = new Agent();
View::share('agent', $agent);
}
public function register()
{
//
}
}
@if ($agent->isMobile())
Show mobile stuff...
@endif
代理包好像已经被放弃了...但是,还有一个更容易使用的替代方案。
https://github.com/riverskies/laravel-mobile-detect
只需安装软件包...
$ composer require riverskies/laravel-mobile-detect
...并开始在 Blade 中使用它。完全没有设置!
@mobile
Show mobile stuff...
@endmobile
@tablet
Show tablet stuff...
@endtablet
@handheld
Show mobile and tablet stuff...
@endhandheld
请注意,您可能必须运行
php artisan view:clear
才能使新的 Blade 指令生效。
我在这里看到同样的问题,基本上想“固定”移动视图目录,而不弄乱我的控制器(如果可能的话)。
执行此操作的一个地方可能是
app/config/views.php
中的配置:
<?php
use Jenssegers\Agent\Agent as Agent;
$Agent = new Agent();
// agent detection influences the view storage path
if ($Agent->isMobile()) {
// you're a mobile device
$viewPath = __DIR__.'/../mobile';
} else {
// you're a desktop device, or something similar
$viewPath = __DIR__.'/../views';
}
return array(
'paths' => array($viewPath),
.....
似乎有效,为您提供了一个完全不同的工作目录。
我将继续试验,因为桌面和移动设备之间可能会有一些重叠,但我们会看到的。
PS:代理〜= Mobile_Detect
您可以在视图文件夹中创建两个文件夹
mobile
、desktop
。这两个文件夹拥有相同的视图(仅文件名)。
├── views
| ├── mobile
| | ├── main.blade.php
| └── desktop
| ├── main.blade.php
然后在控制器内,您可以使用文件夹名称在桌面视图和移动视图(或任何其他视图,如果添加更多视图)之间切换。
您只需通过PHP解析请求的设备即可。您可以使用此项目来做到这一点:http://mobiledetect.net/。
现在你的控制器看起来像:
public function getIndex() {
$detect = new Mobile_Detect;
return View::make( ($detect->isMobile() ? 'mobile' : 'desktop') . '.your-view-name' );
}
将
($detect->isMobile() ? 'mobile' : 'desktop')
重构为辅助/静态函数当然是个好主意。或者将其注册为 before 路由过滤器中的配置项。
正如对已接受答案的评论中所建议的(仅包括移动设备上的移动视图路径并回退到“默认”视图):
<?php
$viewBasePath = realpath(base_path('resources/views'));
$viewsPaths = [$viewBasePath];
$agent = new Jenssegers\Agent\Agent();
if ($agent->isMobile()) {
array_unshift($viewsPaths, $viewBasePath.'/mobile');
}
return [
'paths' => $viewsPaths
...
这样您就可以只覆盖您需要的内容。这对于电子邮件以及当您有多个具有相同 html 的部分视图(无论设备类别如何)时可能会派上用场。
注意:控制器中的用法不会改变。
示例视图:
├── views
| ├── home.blade.php
| ├── posts.blade.php
| ├── post.blade.php
| ├── emails
| | └── subscription.blade.php
| └── partials
| | ├── posts-popular.blade.php
| | ├── banner-ad.blade.php
| | ├── post-comment.blade.php
| ├── mobile
| | ├── home.blade.php
| | ├── partials
| | └── posts-popular.blade.php
在根路径上运行:
composer require mobiledetect/mobiledetectlib
因此,取决于您想在哪里做出决定:
your-app/views/your-view.blade.php
或your-app/routes/web.php
:
//include the file
use Detection\MobileDetect as MobileDetect;
//instance it
$detect = new Mobile_Detect;
// Any mobile device (less tablets)
if ( $detect->isMobile() && !$detect->isTablet()) {
echo "is mobile, do something";
}
// Any tablet device
elseif( $detect->isTablet() ){
echo "is tablet, do something";
}
// Desktop device
else {
echo "is desktop, do something";
}
$agent = new Jenssegers\Agent\Agent();
$view = ($agent->isMobile()) ? “views_mobile”:“视图”;
$viewBasePath = realpath(base_path("资源/{$view}"));
返回[ '路径' => $viewBasePath