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

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?
Reply
#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
#3

(This post was last modified: 02-12-2015, 12:16 AM by freddy. Edit Reason: change code )

(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?


you routes should be like this bro i guess

Code:
$route['default_controller'] = "Users ";
Reply
#4

Quote:I was trying to do something in the routes.php but i couldn't make that.

In routes.php, set the default controller name, lowercase, no spaces in the controller name.
do NOT specify two controllers as you do in your example code, the second one will override the first.


Code:
$route['default_controller'] = 'users';
CI 3.1 Kubuntu 19.04 Apache 5.x&nbsp; Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply
#5

Yup! my mistake was about
Code:
$this->load->model('Users_model');
Inside the model will use modal file name or modal class name?
Reply
#6

(This post was last modified: 02-13-2015, 02:52 AM by twpmarketing.)

(02-12-2015, 11:46 PM)ardavan Wrote: Inside the model will use modal file name or modal class name?

The loader class specifies that the first variable is the model filename with optional relative path.  Note that the filename case can be a problem.  See the example in this quote from the CI 3.0 docs.

From the CI 3.0 docs about loader class library
Quote: model($model[, $name = ''[, $db_conn = FALSE]])

 Parameters:
  • $model (mixed) – Model name or an array containing multiple models
  • $name (string) – Optional object name to assign the model to
  • $db_conn (string) – Optional database configuration group to load
Returns:
CI_Loader instance (method chaining)
Return type:
CI_Loader

$this->load->model('model_name');

If your model is located in a subdirectory, include the relative path from your models directory. For example, if you have a model located at application/models/blog/Queries.php you’ll load it using:

$this->load->model('blog/queries');

If you would like your model assigned to a different object name you can specify it via the second parameter of the loading method:

$this->load->model('model_name', 'fubar');
$this->fubar->method();


I think the docs might should be a little more specific on this point.  The mixing of Capitalized filenames and non-capitalized names used in the library method is not specifically stated, it is only shown by example ( in quote above).
CI 3.1 Kubuntu 19.04 Apache 5.x&nbsp; Mysql 5.x PHP 5.x PHP 7.x
Remember: Obfuscation is a bad thing.
Clarity is desirable over Brevity every time.
Reply
#7

Just before the code example of the Users_model class, you say the name of the php-file is user_model.php
Maybe that's your whole problem!
$this->load->model('users_model') searches for the file Users_model.php inside the application/models folder.
If the file is named user_model.php (in stead of Users_model.php) it won't work.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB