Codeigniter user_agent [英] Codeigniter user_agent

查看:300
本文介绍了Codeigniter user_agent的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我第一次开发响应式网站,我尝试使用CodeIgniter user_agent类。

This is my first time developing a responsive website, and I attempted using the CodeIgniter user_agent class.

我注意到有

is_mobile()

p>

and

is_browser()


$ b b

然而,我想到的图片是平板电脑上的网站看起来非常类似于浏览器,只有移动网站加载不同的视图文件。

但是,is_mobile()包括平板电脑和手机,这不是我所希望的。是否有替代方案?

However, is_mobile() includes both tablets and mobile phones, and it's not what I'm hoping for. Is there an alternative to this?

原因:我使用jQuery移动,我为手机加载一个完全不同的布局,我不想这个视图

Reason: I am using jQuery mobile, and I load a completely different layout for the mobile phone, and I don't want this view to appear on tablets.

推荐答案

您有几个选项。

您可以扩展库并创建检查平板电脑的方法:

You can extend the library and create a method to check for tablet:

class MY_User_agent extends CI_User_agent {

    public function __construct()
    {
        parent::__construct();
    }

    public function is_tablet()
    {
        //logic to check for tablet
    }
}

// usage
$this->load->library('user_agent');
$this->user_agent->is_tablet();

或者您可以覆写现有的 is_mobile()方法以具有您想要的功能:

Or you could override the existing is_mobile() method in the library to have the functionality you want:

class MY_User_agent extends CI_User_agent {

    public function __construct()
    {
        parent::__construct();
    }

    public function is_mobile()
    {
        // you can copy the original method here and modify it to your needs
    }
}

// usage
$this->load->library('user_agent');
$this->user_agent->is_mobile();

http://ellislab.com/codeigniter/user-guide/general/creating_libraries.html

application / libraries / MY_User_agent.php:

application/libraries/MY_User_agent.php:

class MY_User_agent extends CI_User_agent {

    public function __construct()
    {
        parent::__construct();
    }

    public function is_ipad()
    {
        return (bool) strpos($_SERVER['HTTP_USER_AGENT'],'iPad');
            // can add other checks for other tablets
    }
}

控制器:

public function index()
{
    $this->load->library('user_agent');

    ($this->agent->is_ipad() === TRUE) ? $is_ipad = "Yes" : $is_ipad = "No";

    echo "Using ipad: $is_ipad";

}

这篇关于Codeigniter user_agent的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆