Hello,
I work with CodeIgniter since 1 year and after little projects, I want to work on a big project. Of course I choose CodeIgniter for this project because I think it's a very good and powerful framework.
I want to write a good code and use many libraries.
But I don't know if I wrote a good and reusable code.
For example I manage customers in my application and I wrote a library to add, update, delete my customers.
Here is an excerpt of my code :
I have a config file with customer fields and a form validation file.
I have a customer model that looks like this :
With my library, is my approach good ?
Thank
Sorry if my english is not good, I'm french.
I work with CodeIgniter since 1 year and after little projects, I want to work on a big project. Of course I choose CodeIgniter for this project because I think it's a very good and powerful framework.
I want to write a good code and use many libraries.
But I don't know if I wrote a good and reusable code.
For example I manage customers in my application and I wrote a library to add, update, delete my customers.
Here is an excerpt of my code :
PHP Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Customer {
public $CI;
public $rules;
public $customer;
public function __construct($params)
{
$this->CI =& get_instance();
$this->CI->config->load( 'customerConfig' , TRUE );
$this->CI->config->load( 'customerValidationRules' , TRUE );
$this->CI->load->model('customerModel');
$this->rules = config_item('customerRules');
$this->customer = config_item('customer');
}
public function add()
{
$this->CI->load->helper(array('form'));
$this->CI->load->library('form_validation');
$this->CI->form_validation->set_rules($this->rules['add_rules']);
if( $this->CI->form_validation->run() !== FALSE )
{
foreach( $this->customer['customer_fields'] AS $key => $element )
{
if( !$this->CI->input->post($element) )
{
$datas[$key] = NULL;
}
$datas[$key] = $this->CI->input->post($element);
}
$address_id = $this->add_address( $this->customer['address_field'] );
$date = date('Y-m-d H:i:s');
$datas['address'] = $address_id;
$datas['password'] = password_hash( $this->CI->input->post('password'), PASSWORD_BCRYPT);
}
$datas['status'] = 1;
$datas['date_create'] = $date;
$datas['date_update'] = $date;
if( $this->CI->customer_model->add($datas) )
{
$this->CI->session->set_flashdata('success', 'Congratulations your customer has been registered');
return TRUE;
}
$this->CI->session->set_flashdata('error', 'An error has occured, please try again later');
return FALSE;
}
}
I have a config file with customer fields and a form validation file.
I have a customer model that looks like this :
PHP Code:
<?php
defined('BASEPATH') OR exit('No direct script access allowed');
class Customer_model extends CI_Model {
public function __construct()
{
$this->load->database();
}
public function add($data)
{
return $this->db->insert('customer', $data);
}
}
With my library, is my approach good ?
Thank
Sorry if my english is not good, I'm french.