Various kinds of service in a code igniter

I am new to code igniter. Is there a best practice for serving different types for different contexts? For example, I would like to serve specific pages for mobile user agents with the same controllers.

+2
source share
1 answer

There is no hard and fast rule for this. You can structure your view files as you like and call $this->load->view()to load different view files for different results in your controller. In my experience, CodeIgniter is very easy to adapt to how you organize your application files.


, , system/application/views : main mobile :

system/
  application/
    views/
      main/
        index.php
        some_page.php
        ...
      mobile/
        index.php
        some_page.php
        ...

, , , , , main mobile , .

, , ...

// Place this just below the controller class definition
var $view_type = 'main';

// Controller constructor
function MyController()
{
    parent::Controller();

    if ($this->agent->is_mobile())
    {
        $this->view_type = 'mobile';
    }
    else
    {
        $this->view_type = 'main';
    }
}

// Example action
function some_page()
{
    // ...

    // This comes from the 'var $view_type;' line above
    $this->load->view($this->view_type . '/some_page');
}

:

, , , CodeIgniter:)

+9

All Articles