• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
A login script that works great in regular Codeigniter will not work in HMVC

#1
I am building a shopping cart in hmvc but every time I build my login script in regular codeigniter it works great but every time I switch it over to HMVC something goes wrong and I keep getting a 404 page not fiind when I click the login in button. Here is the controllers I am using and and models and views for login.  First my Login view.

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends MX_Controller {

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

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

}

Then my home contoller

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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 my verify controller

Code:
<?php if ( ! defined('BASEPATH')) exit('No direct script access allowed');

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;
  }
}
}

Then my model

Code:
<?php  if (!defined('BASEPATH')) exit('No direct script access allowed');

Class User extends CI_Model {
   
function login($username, $password)
{
  $this -> db -> select('id, username, password');
  $this -> db -> from('users');
  $this -> db -> where('username', $username);
  $this -> db -> where('password', MD5($password));
  $this -> db -> limit(1);

  $query = $this -> db -> get();

  if($query -> num_rows() == 1)
  {
    return $query->result();
  }
  else
  {
    return false;
  }
}
}

and my views first login view

Code:
<!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> Login with CodeIgniter</title>
</head>
<body>
  <h1> Login</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>
 Then home view
Code:
<!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>Login - Private Area</title>
</head>
<body>
  <h1>Home</h1>
  <h2>Welcome <?php echo $username; ?>!</h2>
  <a href="home/logout">Logout</a>
</body>
</html>

I do not know what to think when I run this script on regular codeigniter it works fine but when I run it on HMVC I get an 404 errror saying verify is not found. all help greatly appreciated. I do not know what possibly could change by going from regular codeigniter to HMVC. all help greatly appreciated.
Reply

#2
I found the answer to my question it is all in the routes you have to name the different controllers as such $route['verify'] = 'modulename/verify/index'; this solved my problem. I do not know if you have to do this for each module you add or not.
Reply

#3
(06-30-2015, 02:05 PM)Codin2015 Wrote: I found the answer to my question it is all in the routes you have to name the different controllers as such $route['verify'] = 'modulename/verify/index'; this solved my problem. I do not know if you have to do this for each module you add or not.


Did you add this to your ./application/config/config.php ?

PHP Code:
/*
|--------------------------------------------------------------------------
| HMVC Module paths
|--------------------------------------------------------------------------
|
*/
$config['modules_locations'] = array(
    
APPPATH.'modules/' => '../modules/',
); 
What did you Try? What did you Get? What did you Expect?

Joined CodeIgniter Community 2009.  ( Skype: insitfx )
Reply

#4
(06-30-2015, 09:24 PM)InsiteFX Wrote:
(06-30-2015, 02:05 PM)Codin2015 Wrote: I found the answer to my question it is all in the routes you have to name the different controllers as such $route['verify'] = 'modulename/verify/index'; this solved my problem. I do not know if you have to do this for each module you add or not.


Did you add this to your ./application/config/config.php ?


PHP Code:
/*
|--------------------------------------------------------------------------
| HMVC Module paths
|--------------------------------------------------------------------------
|
*/
$config['modules_locations'] = array(
 
APPPATH.'modules/' => '../modules/',
);


No I did not I am still having trouble getting my pages found so I will try this thank you
Reply

#5
No I did not but I will try that next thank you.
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2020 MyBB Group.