Welcome Guest, Not a member yet? Register   Sign In
Confused about URI segments and functions
#1

[eluser]dallen33[/eluser]
I'm still "new" to this, so bare with me.

I'm using DX_Auth and I want the admin to be able to edit users in the backend. So the users function looks like this in the backend controller:
Code:
function users()
    {
        $this->load->model('dx_auth/users', 'users');            
        
        // Search checkbox in post array
        foreach ($_POST as $key => $value)
        {
            // If checkbox found
            if (substr($key, 0, 9) == 'checkbox_')
            {
                // If ban button pressed
                if (isset($_POST['ban']))
                {
                    // Ban user based on checkbox value (id)
                    $this->users->ban_user($value);
                }
                // If unban button pressed
                else if (isset($_POST['unban']))
                {
                    // Unban user
                    $this->users->unban_user($value);
                }
                if (isset($_POST['change_role']))
                {
                    $role = $_POST['role_parent'];
                    $this->users->change_role($value,$role);
                }
                
                if (isset($_POST['change_email']))
                {
                    $email = $_POST['email'];
                    $this->users->change_email($value,$email);
                }

                else if (isset($_POST['reset_pass']))
                {
                    // Set default message
                    $data['reset_message'] = 'Reset password failed';
                
                    // Get user and check if User ID exist
                    if ($query = $this->users->get_user_by_id($value) AND $query->num_rows() == 1)
                    {        
                        // Get user record                
                        $user = $query->row();
                        
                        // Create new key, password and send email to user
                        if ($this->dx_auth->forgot_password($user->username))
                        {
                            // Query once again, because the database is updated after calling forgot_password.
                            $query = $this->users->get_user_by_id($value);
                            // Get user record
                            $user = $query->row();
                                                        
                            // Reset the password
                            if ($this->dx_auth->reset_password($user->username, $user->newpass_key))
                            {                            
                                $data['reset_message'] = 'Reset password success';
                            }
                        }
                    }
                }
            }                
        }
        
        $this->load->model('dx_auth/roles', 'roles');
        
        /* Database related */
                    
        // If Add role button pressed
        if ($this->input->post('add'))
        {
            // Create role
            $this->roles->create_role($this->input->post('role_name'), $this->input->post('role_parent'));
        }
        else if ($this->input->post('delete'))
        {                
            // Loop trough $_POST array and delete checked checkbox
            foreach ($_POST as $key => $value)
            {
                // If checkbox found
                if (substr($key, 0, 9) == 'checkbox_')
                {
                    // Delete role
                    $this->roles->delete_role($value);
                }                
            }
        }
        
        /* Showing page to user */
        
        // Get offset and limit for page viewing
        $offset = (int) $this->uri->segment(3);
        // Number of record showing per page
        $row_count = 10;
        
        // Get all users
        $data['users'] = $this->users->get_all($offset, $row_count)->result();
        
        // Pagination config
        $p_config['base_url'] = '/backend/users/';
        $p_config['uri_segment'] = 3;
        $p_config['num_links'] = 2;
        $p_config['total_rows'] = $this->users->get_all()->num_rows();
        $p_config['per_page'] = $row_count;
                
        // Init pagination
        $this->pagination->initialize($p_config);        
        // Create pagination links
        $data['pagination'] = $this->pagination->create_links();
        $data['roles'] = $this->roles->get_all()->result();
        
        // Load view
        $this->load->view('backend/users', $data);
    }
The URL looks like this:

http://localhost:8888/index.php/backend/users

When that page is loaded, a table is presented with all users and a link to edit a user, which looks like this:

http://localhost:8888/index.php/backend/users/edit/1

'1' being the userid. I just don't understand how I build this in the controller. I could easily do another function called edit, but I'd rather keep everything in the users function.
#2

[eluser]Mackstar[/eluser]
That is a long function! I understand what you are trying to do, but as the function becomes even longer you will have even more trouble trying to manage it as each of your commands become too reliant on each other.

This ends up turning into Spagetti. Your clean code and well commented syntax is the only thing that is saving you at the moment.

I would separate ALL of your actions into separate functions, then any consistent commands that you see in many of your functions you can add as a method in the model where possible or create a local method.

The controller needs to be as simple as possible, models need to contain ALL of your data logic. After 2 years heavy MVC usage I have found these techniques to be the most manageable and maintainable.

But that is my opinion, and preference. Others may disagree with me, but that is what I would do...

CI's controller centric documentation does not bring out MVC's and CI's power.

Cheers

Richard




Theme © iAndrew 2016 - Forum software by © MyBB