我知道以前在这里已经问过很多很多次了,但我正在尝试优化我们公司的移动网关。这是在2016年外包的,但开始构建它的人并不是为移动构建它。他没有完成。现在,请知道我咬牙切齿 ASP.Net 所以PHP不是我的强项,但我继承了这个项目来完成它。它是用CodeIgniter 2.2构建的,我无法升级它。所有移动(我所从事的(都将使用 Bootstrap(最新版本(构建,因为我喜欢 Bootstrap 以及它使构建"移动优先"的东西变得多么容易。所以我不想尝试更改他构建的所有布局和文件,因为所有浏览器都运行良好并且显示效果非常好,但不适用于移动设备。还要知道我已经查看了我能在这里找到的每个示例(SO(。我已经尝试了一切。问题是,我可以显示我的"移动"登录视图,但我不知道从那里去哪里。我是否只为要构建的移动页面创建单独的模型和控制器?我将在下面展示我尝试过的示例代码以及有效的代码。有没有人尝试过这样的事情并有一个工作的例子?至少有足够的代码可以指出我正确的方向?我将不胜感激。在这里绞尽脑汁。哦,是的,我确实更新了最新版本的 CodeIgniter (3.1.11( 的user_agent代码,以捕获所有最新的移动浏览器,因为我知道 CI 2.2 有点过时了。顺便说一句,如果您使用普通计算机访问,我试图提供尽可能多的信息,因为它会显示常规登录页面并且登录正常。但是,如果您使用移动设备访问,它会显示移动登录页面,但不会登录。只需刷新页面。无论我是否走user_agent路线并使用相同的 URL,或者我是否走Detect_Mobile.php路线并加载 mobi.gatewayurl.com 页面。 好的,这是主机上的布局。抱歉,我知道这很复杂,但是所有这些布局都已经完成,我添加的只是移动和mobi目录。
|--public_html
|--gateway
|--gw_application
|--controllers
|--account.php
|--administrator.php
|--login.php
|--core
|--MY_Loader.php
|--helpers
|--hooks
|--models
|--account_model.php
|--administrator_model.php
|--login_model.php
|--other models as well
|--third_party
|--Mobile_Detect.php <== Was also playing around with this as well.
|--views
|--account
|--dashboard.php
|--All the other views for the regular users.
|--administrator <== Depending on what role they play. A few users will be an Administrator.
|--dashboard.php
|--All the other views for the Administrators. Which should only be a few people. Most will fall under "account".
|--mobile <== Do I put another set of models and controllers in here?
|--account <== Copied all views from the account directory for regular users in here as well.
|--templates <== The mobile login page loads by putting in a blank header.php, heading.php, and footer.php in here. Otherwise it throws errors if I don't have these in here.
|--login.php <== Or I tried even naming it mlogin.php for mobile, but it won't login. It loads, but just refreshes the page.
|--templates
|--admin
|--header.php
|--heading.php
|--footer.php
|--header.php
|--heading.php
|--footer.php
|--login.php <== The MAIN login page when visiting through a normal browser.
|--gw-system
|--core
|--blah blah
|--mobi <== Also this works using Detect_Mobile.php redirecting it to here with the URL as https://mobi.ourgatewayurl.com.
这是我从SO文章13928677获得的MY_Loader.php文件。
<?php if (! defined('BASEPATH')) exit('No direct script access allowed');
class MY_Loader extends CI_Loader
{
//overides existing view function
function view($view, $vars = array(), $return = FALSE)
{
$CI =& get_instance();
$CI->load->library("user_agent");
if($CI->agent->is_mobile()){
$view = 'mobile/'.$view;// <== This does diplay my mobile login page without changing the URL.
}
return $this->_ci_load(array('_ci_view' => $view, '_ci_vars' => $this->_ci_object_to_array($vars), '_ci_return' => $return));
}
}
//This works good to keep the same URL, such as https://gateway.ourgatewayurl.com, but where to go from here? Otherwise using Mobile_Detect.php, I
//can redirect to https://mobi.ourgatewayurl.com. That would be no problem as well, I can do the subdomain thing, but again, where to go from here?
?>
这是登录.php控制器。再说一次,我没有写这个,它已经完成了。
<?php
class Login extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->model('login_model');
$this->load->model('setting_model');
$this->load->model('producer_model');
$this->load->model('company_model');
$this->load->model('report_model');
$this->load->model('administrator_model');
}
public function index()
{
$data['settings'] = $this->setting_model->get_setting();
$data['bad_login'] = 0;
if ($this->input->post('username')) {
$data['login'] = $this->login_model->get_login($this->input->post('username'),$this->input->post('password'));
if ($data['login'] == '0') {
$level = $this->session->userdata('user_level');
if ($level == 'administrator') {
$data['message_list'] = $this->administrator_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('administrator/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('administrator/leftmenu', $data);
} else {
$data['company'] = $this->company_model->get_company($this->session->userdata('company_id'));
$data['producer'] = $this->producer_model->get_producer($this->session->userdata('producer_id'));
$data['message_list'] = $this->administrator_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('account/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('account/leftmenu', $data);
}
} else {
$data['bad_login'] = 1;
$data['login'] = '1';
$this->load->view('templates/heading', $data);
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer', $data);
}
} else {
$data['login'] = '1';
$this->load->view('templates/heading', $data);
$this->load->view('templates/header', $data);
$this->load->view('login', $data);
$this->load->view('templates/footer', $data);
}
}
}
?>
这是帐户的一部分.php控制器。我漏掉了很多。
<?php
//Only showing for index and dashboard, but he has a public function for every page he has on the gateway.
class Account extends CI_Controller {
public function __construct()
{
parent::__construct();
$this->load->library('session');
$this->load->library('My_PHPMailer');
$this->load->model('account_model');
$this->load->model('setting_model');
$this->load->model('user_model');
$this->load->model('producer_model');
$this->load->model('company_model');
$this->load->model('email_model');
$this->load->model('report_model');
$this->load->model('pagenate_model');
$this->load->model('staff_model');
$this->load->model('signup_model');
if (!$this->session->userdata('user_id') || ($this->session->userdata('user_level') != 'corporate' && $this->session->userdata('user_level') != 'branch' && $this->session->userdata('user_level') != 'producer')) {
header("Location: " . base_url() . 'index.php?/login/logout/');
die();
}
}
public function index()
{
$data['settings'] = $this->setting_model->get_setting();
$data['company'] = $this->company_model->get_company($this->session->userdata('company_id'));
$data['producer'] = $this->producer_model->get_producer($this->session->userdata('producer_id'));
$data['message_list'] = $this->account_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('account/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('account/leftmenu', $data);
}
public function dashboard()
{
$data['settings'] = $this->setting_model->get_setting();
$data['company'] = $this->company_model->get_company($this->session->userdata('company_id'));
$data['producer'] = $this->producer_model->get_producer($this->session->userdata('producer_id'));
$data['message_list'] = $this->account_model->get_message();
$this->load->view('templates/admin/heading', $data);
$this->load->view('templates/admin/header', $data);
$this->load->view('account/dashboard', $data);
$this->load->view('templates/admin/footer', $data);
$this->load->view('account/leftmenu', $data);
}
}
?>
这是登录模型。
<?php
class Login_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function get_login($username, $password)
{
$this -> db -> select('*');
$this -> db -> from('user');
$this -> db -> where('user_email = ' . "'" . $this->db->escape_str($username) . "'");
$this -> db -> where('user_password = ' . "'" . $this->db->escape_str(do_hash($password, 'md5')) . "'");
$this -> db -> where('user_is_deleted = 0');
$this -> db -> limit(1);
$query = $this -> db -> get();
$user = $query->row_array();
if($query -> num_rows() == 1) {
$this -> db -> select('*');
$this -> db -> from('user_level');
$this -> db -> where('user_level_id = ' . $user['user_level_id']);
$this -> db -> where('user_level_is_deleted = 0');
$this -> db -> limit(1);
$query2 = $this -> db -> get();
$user_level = $query2->row_array();
$pid = '';
$coid = '';
$ctype = '';
if ($user_level['user_level_name'] != 'administrator') {
$this -> db -> select('*');
$this -> db -> from('producer');
$this -> db -> where('user_id = ' . $user['user_id']);
$this -> db -> limit(1);
$query2 = $this -> db -> get();
$producer = $query2->row_array();
$this -> db -> select('*');
$this -> db -> from('company');
$this -> db -> where('company_id = ' . $producer['company_id']);
$this -> db -> limit(1);
$query3 = $this -> db -> get();
$company = $query3->row_array();
$pid = $producer['producer_id'];
$coid = $producer['company_id'];
$ctype = $company['company_type_id'];
}
$newdata = array(
'user_id' => $user['user_id'],
'producer_id' => $pid,
'company_id' => $coid,
'company_type_id' => $ctype,
'user_level_id' => $user['user_level_id'],
'user_level' => $user_level['user_level_name'],
'user_name' => $user['user_first_name'] . ' ' . $user['user_last_name']
);
$this->session->set_userdata($newdata);
return '0';
} else {
$_SESSION['user_id'] = '';
$_SESSION['producer_id'] = '';
$_SESSION['company_id'] = '';
$_SESSION['company_type_id'] = '';
$_SESSION['user_level_id'] = '';
$_SESSION['user_level'] = '';
$_SESSION['user_name'] = '';
return '1';
}
}
}
?>
对不起,伙计们,这太长了。我只是想让每个人都看看这是如何布局的,以及他是如何编写代码的。任何帮助将不胜感激。非常感谢。
没关系,我决定将整个事情转换为Bootstrap。如果我这样做会更好。无论如何,它都需要。无论如何,Bootstrap对移动设备都很好。