A
A
Alexey Verkhovtsev2016-12-10 11:55:23
CodeIgniter
Alexey Verkhovtsev, 2016-12-10 11:55:23

What is the best way to include libraries in Codeigniter?

Hello everyone, I'm making a website, in almost 90% of controllers and models the same libraries and helpers are cut, I think it can stuff everything into autoload, who tested this option in time, does it load everything more or not?
And yet, I wanted to know the opinion about my code using the example of one method of the controller and the model, maybe what I’m doing is not quite right and I can do it much better.
Pages controller, it has a method responsible for the recruiting page

public function recruiting() {
      
        if( !file_exists(APPPATH . '/views/user/pages/recruiting.php') )
            show_404();
        
        $page_id = 4;
        
        if($_POST) {
            $request_type = 2;
            $this->requests_model->add_request($request_type);
            redirect('/recruiting/');
        }
        
        $data['language'] = $this->language;
        $data['usd'] = $this->USD;
        $data['eur'] = $this->EUR;
        $data['menu_active'] = 'recruiting';
        $data['meta_tags'] = $this->meta_tags_model->get_the_meta_tags($page_id);
        $data['page_fields'] = $this->page_fields_model->get_the_page_fields($page_id);
        $data['fields'] = $this->fields_model->get_the_fields();
        $data['menu'] = $this->menu_model->get_the_menu();
        
        $this->load->view('user/inc/top-with-meta', $data);
        $this->load->view('user/inc/header', $data);
        $this->load->view('user/pages/recruiting', $data);
        $this->load->view('user/inc/footer', $data);
        $this->data_processing->clear_session();
        
    }

well, the Requests_model with the add_request method
public function add_request($request_type) {
   
        $email_to = '[email protected]';
        
        add_an_array_to_session($_POST);
        
        $fields = $this->fields_model->get_the_fields();
        
        if($request_type === 2) {
            
            $this->form_validation->set_rules('full_name', 'Full Name', 'trim|required');
            $this->form_validation->set_rules('email', 'E-mail', 'trim|required');
            $this->form_validation->set_rules('phone', 'phone', 'trim|required');
            $this->form_validation->set_rules('address', 'Address', 'trim|required');
            
            if( $this->form_validation->run() === FALSE || empty($_FILES['file']['name']) ) {
                
                error( $fields[14]['value_' . $this->language] );
                return false;
            
            } else {
                
                if( !valid_email( $this->input->post('email') ) ) {
                    
                    error( $fields[16]['value_' . $this->language] );
                    return false;
                
                }
                
                $config['upload_path'] = './uploads/requests/';
                $config['allowed_types'] = 'doc|pdf|jpeg|jpg|png';
                $config['max_size'] = 4096;
                $config['file_name'] = $this->data_processing->new_file_name( $_FILES['file']['name'] );
                $this->load->library('upload', $config);
                
                if( !$this->upload->do_upload('file') ) {
                    
                    error( $fields[13]['value_' . $this->language] );
                    return false;
                    
                } else {
                    
                    $upload_data = $this->upload->data();

                    $full_name = $this->data_processing->cleaning_of_empty_data( $this->input->post('full_name') );
                    $email = $this->data_processing->cleaning_of_empty_data( $this->input->post('email') );
                    $phone = $this->data_processing->cleaning_of_empty_data( $this->input->post('phone') );
                    $address = $this->data_processing->cleaning_of_empty_data( $this->input->post('address') );
                    $text = $this->data_processing->cleaning_of_empty_data( $this->input->post('message') );
                    $file_name = $upload_data['file_name'];
                    $date = time();

                    $subject = 'Careers';
                    $from_page = 'FedPay';
                    $message_mail = '';
                    $message_mail .= "Full Name: $full_name\n\r";
                    $message_mail .= "E-mail: $email\n\r";
                    $message_mail .= "Phone: $phone\n\r";
                    $message_mail .= "Address: $address\n\r";
                    $message_mail .= "Additional information and CV: $text\n\r";
                    
                    $data = array(
                        'full_name' => $full_name,
                        'email' => $email,
                        'phone' => $phone,
                        'address' => $address,
                        'request_type_id' => $request_type
                    );

                    $query = $this->db->insert($this->name_table, $data);
                    
                    if( !$query ) {
                        
                        unlink('./uploads/requests/' . $file_name);
                        error( $fields[12]['value_' . $this->language] );
                        return false;
                        
                    }
                    
                    $request_id = $this->db->insert_id();
                    
                    $data = array(
                        'text' => $text,
                        'request_id' => $request_id,
                        'date' => $date,
                        'file_name' => $file_name
                    );
                    
                    $query = $this->db->insert('messages', $data);
                    
                    if( !$query ) {
                        
                        unlink('./uploads/requests/' . $file_name);
                        $this->delete_request($request_id);
                        error( $fields[12]['value_' . $this->language] );
                        return false;
                        
                    }

                    if( !$this->sending_request_message($email, $from_page, $email_to, $subject, $message_mail, $file_name) ) {

                        unlink('./uploads/requests/' . $file_name);
                        $this->delete_request($request_id);
                        $this->delete_messages_from_request($request_id);
                        error( $fields[12]['value_' . $this->language] );
                        return false;

                    }

                    unset($_SESSION['validation_form']);
                    success( $fields[15]['value_' . $this->language] );
                    return true;
                    
                }
                
            }
            
        }
        
    }

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
Egor, 2016-12-10
@egormmm

1. If a component is often used, connect it via autoload. So the code will be cleaner and clearer. Those extra ms to load do not matter.
2. Accept data from post via $this->input->posts('name'). Have a look at the built-in library input-.
3. Do not check for the presence of the view file. If the file does not exist, the error page will come out by itself.
4. Header and footer are better connected in the view file, and not in the controller.
5. Well, actually you have everything in a heap in the model. All actions must be separated into separate methods. Or even Objects.

M
MrKMV34, 2016-12-10
@MrKMV34

Libraries can be added to the class constructor if they are used in almost all actions of the controller

public function __construct(){
    parent::_construct();
    $this->load->model(array(....));
    $this->load->helper(array(....));
}

and your method in the model is overloaded with functionality. No need to send email from the model

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question