I borrowed / stole this class loading method from phodexcel codeigniter integration.
Download the library from http://mobiledetect.net , but put Mobile_Detect.php in 'third_party', then create MobileDetect.php in 'library' and put the following code in it:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed'); require_once APPPATH."third_party/Mobile_Detect.php"; class MobileDetect extends Mobile_Detect { public function __construct() { parent::__construct(); } }
Now you can use it in your controllers, for example:
$this->load->library('MobileDetect'); if ($this->mobiledetect->isMobile()) {
I am sure that there are other (even better) ways to integrate mobiledetect into codeigniter, I just wanted to share how I did it, I hope that it will be useful.
A few notes:
1) You do not need to use the MobileDetect.php stub file, if you put Mobile_Detect.php directly into the "libraries", you can still use it without $detect = new Mobile_Detect(); instead, call functions that like it: $this->mobile_detect->isMobile()
2) The name of the stub class can be anything if you follow the recommendations of CodeIgniter. For example, you can use "MD" as the class name and then refer to it using $this->md->isMobile()
3) I recommend adding if ( ! defined('BASEPATH')) exit('No direct script access allowed'); after opening <?php Mobile_Detect.php to prevent direct access to the class.
source share