เนื้อหาต่อไปนี้ เป็นแนวทางการนำ Mobile Detect PHP class มาใช้ codeigniter
จากเนื้อหา
แนะนำ Mobile Detect php class พร้อมใช้ เช็คการใช้งาน ผ่าน มือถือ แท็บเล็ต
https://www.ninenik.com/content.php?arti_id=437 via @ninenik
เว็บไซต์ http://mobiledetect.net/
เราจะทำการนำ PHP class ดังกล่าวข้างต้น มาใช้งานร่วมกับ codeigniter อย่าง
เริ่มต้น ให้ไปที่เว็บไซต์
ดาวน์โหลดไฟล์ PHP Class แล้วแตกไฟล์ นำไฟล์ที่ชือ Mobile_Detect.php มาใช้งาน
โดยให้ทำการ copy ไปไว้ในโฟลเดอร์ที่ชื่อ apps > third_party
จากนั้นให้สร้างไฟล์ชื่อ MobileDetect.php ไว้ในโฟลเดอร์ apps > libraries
โดยมีรูปแบบโค้ดดังนี้
1 2 3 4 5 6 7 8 9 | <?php if (!defined( 'BASEPATH' )) exit ( 'No direct script access allowed' ); require_once APPPATH. "/third_party/Mobile_Detect.php" ; class MobileDetect { public function __construct() { } } |
สำหรับวิธีการเรียกใช้งานทำได้ดังนี้
ทดสอบด้วยไฟล์ตัวอย่าง
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 | <?php defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); class Example extends CI_Controller { public function __construct() { parent::__construct(); $this ->load->library( 'MobileDetect' ); } public function index() { $detect = new Mobile_Detect; } } |
วิธีการใช้งานเราสามารถดูวิธีการใช้งานและรูปแบบได้จากเว็บไซต์
ตัวอย่างการใช้งานอย่างง่าย
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 | <?php defined( 'BASEPATH' ) OR exit ( 'No direct script access allowed' ); class Example extends CI_Controller { public function __construct() { parent::__construct(); $this ->load->library( 'MobileDetect' ); } public function index() { $detect = new Mobile_Detect; if ( $detect ->isMobile()){ $this ->load->view( 'mobile/index' ); } else { $this ->load->view( 'site/index' ); } } } |