dir:
application -controllers -models -views -mobile_views
如何在mobile_views
我自動加載模板時,我使用$this->load->view
並通過iPhone或其他手機的看法?如何在codeigniter中通過代理自動加載移動模板?
dir:
application -controllers -models -views -mobile_views
如何在mobile_views
我自動加載模板時,我使用$this->load->view
並通過iPhone或其他手機的看法?如何在codeigniter中通過代理自動加載移動模板?
檢查this
您可以通過兩種方式做到這一點。方式1:非常簡單。在上面的答案(我給的鏈接)添加以下行MyController
函數結束
$this->load->_ci_view_path . = $this->view_type .'/';
你完成了。您可以像普通視圖加載一樣簡單地加載視圖。
方式2: 要自動加載基於用戶代理的視圖,我認爲你可以使用鉤子來實現它。爲了實現這個鉤就需要按照下面的步驟在autoload.php
$自動加載[ '庫']
自動加載用戶代理程序庫=陣列( 'USER_AGENT');
在config.php
$配置[ 'enable_hooks'] = TRUE啓用鉤;
不在post_controller_constructor
上實施掛鉤。添加以下代碼來hooks.php
$鉤[ 'post_controller_constructor'] [] =陣列( '類'=> '的loadView', '功能'=> '負載', '文件名'=>「loadview.php ', 'filepath'=>'hooks' );
現在創建一個名爲loadview.php
頁面掛鉤目錄下有下面的代碼
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 .'/'; } }
從「意見」除了加載從另一個目錄的意見,我發現這個論壇的主題是有幫助的
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);
}
}
,你可以從控制器的工作休息。
我這樣做是在我的控制器:
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');
}
這樣我可以在不同的數據移動和網頁添加。
我還擴展了默認的控制器,並添加了一些有用的額外功能:
這樣我就可以更好地管理不同的頁面。
再見!