How To Load Different View For Mobile Devices In
CodeIgniter 3 - PHP
User Agent Class
The User Agent Class provides functions that help identify information about the browser, mobile device, or robot visiting your site. In addition, you can get referrer information as well as language and supported character-set information.
The User Agent class is initialized in your controller using the $this->load->library function:
$this->load->library('user_agent');The name of the web browser viewing your site:
$browser = $this->agent->browser();
> Home Controller
<?php defined('BASEPATH') OR exit('No direct script access allowed');class Home extends CI_Controller {public function index(){// Load User Agent Library [OR autoload.php file]$this->load->library('user_agent');if ($this->agent->is_mobile()) {// Pass Data in View File$data_array['title'] = "Mobile Page Title";// Mobile View Template$this->load->view('home/_mobile/index', $data_array);} else {// Pass Data in View File$data_array['title'] = "Page Title";// Other Plateform View Template$this->load->view('home/index', $data_array);}}}
> Main View File
> Mobile View File<h1>Main View Template</h1><small><?= $title ?></small><br><?php$is_mobile = $this->agent->is_mobile();var_dump($is_mobile);?>
<h1>Mobile View Template</h1><small><?= $title ?></small><br><?php$is_mobile = $this->agent->is_mobile();var_dump($is_mobile);?>
Comments
Post a Comment