Welcome Guest, Not a member yet? Register   Sign In
How to make new controller as default (run on index page)
#2

(02-11-2015, 08:47 PM)ardavan Wrote: I created users.php in controller folder and user_model.php in model folder and new_user.php in view/users folder.
Now i wanna make new_users as default instead of welcome message on index page
I was trying to do something in the routes.php but i couldn't make that.

users.php

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

class Users extends CI_Controller{

 function __construct() {
   parent::__construct();
   $this->load->helper('form');
   $this->load->helper('url');
   $this->load->helper('security');
   $this->load->model('Users_model');
   $this->load->database();
 }

 public function index() {
   redirect('users/view_users');
 }

 public function view_users() {
   $data['query'] = $this->Users_model->get_all_users();
   $this->load->view('users/view_all_users', $data);
 }

 public function new_user() {
   //load support assests
   $this->load->library('form_validation');
   $this->form_validation->set_error_delimiters('','<br>');

   //Set validation rules
   $this->form_validation->set_rules('first_name', 'First Name', 'required|min_length[1]|max_length[255]');
   $this->form_validation->set_rules('last_name', 'Last Name', 'required|min_length[1]|max_length[125]');

   $this->form_validation->set_rules('email', 'Email', 'required|min_length[1]|max_length[255]|valid_email');
   $this->form_validation->set_rules('is_active', 'Is active', 'min_length[1]|max_length[1]|integer|is_natural');

   //Begin validation
   if ( $this->form_validation->run() == FALSE ) {
     //First load, or problem with form
     $data['first_name'] = array( 'name' => 'first_name', 'id' => 'first_name', 'value' => set_value('first_name',''), 'maxlength' => '100', 'size' => '35' );
     $data['last_name']  = array( 'name' => 'last_name' , 'id' => 'last_name' , 'value' => set_value('last_name' ,''), 'maxlength' => '100', 'size' => '35' );
     $data['email'] = array( 'name' => 'email', 'id' => 'email', 'value' => set_value('email',''), 'maxlength' => '100', 'size' => '35' );

     $data['is_active'] = array( 'name' => 'is_active', 'id' => 'is_active', 'value' => set_value('is_active','') );

     $this->load->view('users/new_user', $data);
   }else{
     //validation passed, now escape the data
     $data = array(
             'first_name'  =>  $this->input->post('first_name'),
             'last_name'   =>  $this->input->post('last_name'),
             'email'       =>  $this->input->post('email'),
             'is_active'   =>  $this->input->post('is_active')
             );
     if ( $this->User_model->process_create_user($data) ) {
       redirect('users');
     }
   }
 }

}


user_model.php

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

class Users_model extends CI_Model {

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

public function get_all_users() {
return $this->db->get('users');
}

public function process_create_user($data) {
if ( $this->db->insert('users', $data)) {
return true;
}else{
return false;
}
}

}

new_user.php

Code:
<?php echo form_open('users/new_users'); ?>
<?php if ( validation_errors() ) : ?>
<h3>Whoops! There is an error:</h3>
<p><?php echo validation_errors(); ?></p>
<?php endif; ?>

<table border="0">
<tr>
<td>User First Name</td>
<td><?php echo form_input($first_name); ?></td>
</tr>
<tr>
<td>User Last Name</td>
<td><?php echo form_input($last_name); ?></td>
</tr>
<tr>
<td>User Email</td>
<td><?php echo form_input($email); ?></td>
</tr>
<tr>
<td>User Is Active?</td>
<td><?php echo form_checkbox($is_active); ?></td>
</tr>
</table>

<?php echo form_submit('submit', 'Create'); ?>
or
<?php echo anchor('users/index', 'cancel'); ?>

<?php echo form_close(); ?>


My routes.php changes
But doesn't work at all. ill get "Unable to locate the model you have specified: users_model" error

Code:
$route['default_controller'] = "users/new_user";
$route['default_controller'] = "new_user";

How can i make it?
Load the model using Lowercase
Code:
change  $this->load->model('Users_model'); to  $this->load->model('users_model');
Reply


Messages In This Thread
RE: How to make new controller as default (run on index page) - by arulraj - 02-11-2015, 10:41 PM



Theme © iAndrew 2016 - Forum software by © MyBB