How to automatically download mobile templates by agent in codeigniter?

dir:
application
 -controllers 
 -models
 -views
 -mobile_views

How do I download templates mobile_viewswhen I use $this->load->viewand browse an iphone or other mobile phone?

+3
source share
3 answers

Mark this

You can do this in two ways. Way 1: It is very simple. In the above answer (the link I gave) add the following line at the end of the functionMyController

$this->load->_ci_view_path  . = $this->view_type .'/';

You are done. You can just load the view, just like a regular download.

2: , , . .

  • Autoload autoload.php

    $autoload ['libraries'] = array ('user_agent');

  • config.php

    $config ['enable_hooks'] = TRUE;

  • post_controller_constructor. hooks.php

    $hook ['post_controller_constructor'] [] = array ('class' = > 'Loadview',                                   'function' = > 'load',                                   'filename' = > 'loadview.php',                                   'filepath' = > 'hooks'                                  );

  • loadview.php hooks,

class Loadview
{

    public static $MOBILE_PLATFORM = 'mobile';
    public static $DEFAULT_PLATFORM = 'default';

    public function load(){
        $this->CI =& get_instance();
        $view_type = $this->CI->agent->is_mobile() ? self::$MOBILE_PLATFORM : self::$DEFAULT_PLATFORM;
        $this->CI->load->_ci_view_path = $this->CI->load->_ci_view_path . $view_type .'/';
    }

}
  1. . , .
+1

"",

http://codeigniter.com/forums/viewthread/132960/

function external_view($path, $view, $vars = array(), $return = FALSE)
    {
        $full_path = $path.$view.'.php';
        if (file_exists($full_path)) 
        {
            return $this->_ci_load(array('_ci_path' => $full_path, '_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
        }
        else
        {
            show_error('Unable to load the requested module template file: '.$view);
        }
    } 

.

0

:

public function index()
{   
    if($this->agent->is_mobile())
    {
        $this->load_mobile();   
    }   
    else
    {
        $this->load_web();
    }
}

public function load_mobile()
{
    $this->load->view('mobile/home');
}

public function load_web()
{
    $this->load->view('web/home');
}

This way I can add different data to mobile and web pages.

I also extend the default controller and add some useful additional features:

  • Includes use of homepage / templates.
  • Can add css and javascript files.
  • Uses the _output method to control the output of controllers.
  • Can load relative content in the form of modules (views)

Therefore, I can better manage different pages.

Bye !!

0
source

All Articles