Laravel 检测手机/平板电脑并加载正确的 View

标签 laravel laravel-4

我已经阅读了如何为 View 添加不同的路径或 namespace ,但我认为这对我来说不是一个合适的解决方案。
我想做的是为移动设备设置一个 View 基本路径,为桌面设备设置一个不同的路径,所以在 View Controller 中我不需要做任何改变。

在路由文件中设置路径并且不接触任何 View Controller 会很棒。有什么想法吗?也许只是 Config::set View 路径?

提前致谢! :)

最佳答案

对于 future 正在寻找一种在 View 中检测设备的方法的 Laravel 5 用户;另一种选择是创建一个 ServiceProvier - 然后使用 View::share() - 这将使设备检测$agent在您的所有 View 中可用。

安装 Agent

composer require jenssegers/agent

创建服务提供者
php artisan make:provider AgentServiceProvider

在 config/app.php
App\Providers\AgentServiceProvider::class,

在 app/providers/AgentServiceProvider.php
<?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

关于Laravel 检测手机/平板电脑并加载正确的 View ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23779088/

相关文章:

php - 在 Laravel 中防止并发写入

laravel - 如何避免vue中的 react 性

php - 删除带有外键 Laravel 错误 : General error: 1025 Error on rename 的列

php - Laravel 重写命名路由并采用错误的路由

php - Laravel 5.2 单元测试错误:BadMethodCallException:调用未定义的方法 Illuminate\Database\Query\Builder::make()

events - Laravel 监听器监听多个事件

html - {{HTML::image}} 在 Laravel 中设置宽度和高度

php - Laravel hasManyThrough 与非默认本地键的关系

php - 拦截动态函数调用(如 laravel HasAttribute 特征)

mysql - laravel 高级连接 5 个表,其中 2 个表进行比较