dsfs5420 2015-06-23 21:00 采纳率: 100%
浏览 52
已采纳

Codeigniter登录适用于常规Codeigniter但不适用于Hmvc

I have this login script which works great on regular codeigniter but when I switch it to hmvc codeigniter I start getting an 404 verify not found error. I have three controllers which I will list below. This controller verifies the login info.

class Verify extends MX_Controller {

function __construct()
 {
parent::__construct();
$this->load->model('user','',TRUE);
}

function index()
{
 //This method will have the credentials validation
$this->load->library('form_validation');

$this->form_validation->set_rules('username', 'Username',      'trim|required|xss_clean');
$this->form_validation->set_rules('password', 'Password', 'trim|required|xss_clean|callback_check_database');

if($this->form_validation->run() == FALSE)
{
 //Field validation failed.  User redirected to login page
 $this->load->view('login_view');
}
else
{
 //Go to private area
 redirect('home', 'refresh');
}

}

function check_database($password)
{
 //Field validation succeeded.  Validate against database
 $username = $this->input->post('username');

 //query the database
 $result = $this->user->login($username, $password);

 if($result)
 {
 $sess_array = array();
 foreach($result as $row)
 {
   $sess_array = array(
     'id' => $row->id,
     'username' => $row->username
   );
   $this->session->set_userdata('logged_in', $sess_array);
   }
   return TRUE;
   }
   else
   {
   $this->form_validation->set_message('check_database', 'Invalid username or password');
 return false;
   }
   }
   }

this is my login controller

 class Login extends MX_Controller {

 function __construct()
 {
 parent::__construct();
 }

 function index()
 {
 $this->load->helper(array('form'));
 $this->load->view('login_view');
 }   

 }

then this is the home controller.

 session_start(); //we need to call PHP's session object to access it   through CI

 class Home extends MX_Controller {

 function __construct()
 {
 parent::__construct();
 }

 function index()
 {
  if($this->session->userdata('logged_in'))
 {
 $session_data = $this->session->userdata('logged_in');
 $data['username'] = $session_data['username'];
 $this->load->view('home_view', $data);
 }
 else
 {
 //If no session, redirect to login page
 redirect('login', 'refresh');
 }
 }

 function logout()
 {
  $this->session->unset_userdata('logged_in');
 session_destroy();
 redirect('home', 'refresh');
 }

 }

then this is my home view.

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"     "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<title>Simple Login with CodeIgniter - Private Area</title>
 </head>
 <body>
 <h1>Home</h1>
 <h2>Welcome <?php echo $username; ?>!</h2>
 <a href="home/logout">Logout</a>
 </body>
 </html>

this is my login view

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"  "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
 <title>Simple Login with CodeIgniter</title>
</head>
<body>
<h1>Simple Login with CodeIgniter</h1>
<?php echo validation_errors(); ?>
 <?php echo form_open('verify'); ?>
 <label for="username">Username:</label>
 <input type="text" size="20" id="username" name="username"/>
 <br/>
 <label for="password">Password:</label>
 <input type="password" size="20" id="passowrd" name="password"/>
 <br/>
 <input type="submit" value="Login"/>
 </form>
 </body>
  </html>

I do not know why going from mvc codeigniter to hmvc would make a difference but something goes wrong. All help greatly appreciated.

  • 写回答

1条回答 默认 最新

  • duanqing2209 2015-06-24 01:16
    关注

    All ways number one check is make sure all controllers and models Ucfirst example Verify.php and not verify.php

    If you need to use callbacks in HMVC then you need code below

    Place this in libraries

    <?php
    
    class MY_Form_validation extends CI_Form_validation {
    
    function run($module = '', $group = '') {
        (is_object($module)) AND $this->CI = &$module;
        return parent::run($group);
    }
    
    } 
    

    And then add $this if($this->form_validation->run($this) == FALSE)

    And also xss_clean not working in codeigniter 3

    Please note the HMVC from here https://bitbucket.org/wiredesignz/codeigniter-modular-extensions-hmvc/downloads only for codeigniter 3

    Also make sure your routes are set correct example

    $route['verify'] = 'modulename/verify/index';

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 基于卷积神经网络的声纹识别
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图
  • ¥15 写一个方法checkPerson,入参实体类Person,出参布尔值
  • ¥15 我想咨询一下路面纹理三维点云数据处理的一些问题,上传的坐标文件里是怎么对无序点进行编号的,以及xy坐标在处理的时候是进行整体模型分片处理的吗
  • ¥15 CSAPPattacklab
  • ¥15 一直显示正在等待HID—ISP
  • ¥15 Python turtle 画图
  • ¥15 stm32开发clion时遇到的编译问题