codeigniter - controller load multiple views

2019-08-31 06:23发布

问题:

I made a website but in two versions one for normal users and one for the mobile users and for both I made view page and also with multilanguage options, first I add in controller

public function index()
    {
        if ($this->input->get("lang") =="en")
            $this->load->view('en_signup');
        else
            $this->load->view('ar_signup');
        $this->load->helper('url');
    }
}

I made pages with name of marabic.php and menglish.php for mobile users now first I need to load these pages also but not mix with the original/default view pages, because I already mention java cript in default view page when its detect mobile user it redirect to m.domainname.com now I want to figure out this issue, please suggest.

回答1:

Try this:

public function index()
{
    $this->load->library('user_agent');
    $this->load->helper('url');
    if ($this->input->get("lang") =="en"){
        if ($this->agent->is_mobile()) {
          $this->load->view('menglish');
        } else {
          $this->load->view('en_signup');
        }
    } else {
        if ($this->agent->is_mobile()) {
          $this->load->view('marabic');
        } else {
          $this->load->view('ar_signup');
        }
  }
}


回答2:

You can detect if a user is visiting from a mobile device by using CodeIgniter's User Agent library.

$this->load->library('user_agent');

if ($this->agent->is_mobile()) {
 // Load mobile view
}