Hirazi Webtech

CodeIgniter Onboarding - Part 3 - Managing your Controllers

Contrary to the belief that the logic of an application should go in the Controller, we at Hirazi Webtech use them as the junction of Models and Views, specially in our projects using CodeIgniter. The junction is provided by objects, introduced in part 1 of this series. The data object layer provides an apt template for getters and setters for the database tables as well as segregating the reusable functionality of a particular module.

 

Without wasting too much of time, we will jump into an example of how to use Controllers in CodeIgniter:

 

class Booking extends CI_Controller {
public function __construct() {
parent::__construct ();
$this->load->model ( 'booking_model' );
$this->load->model ( 'slot_model' );
$this->load->model ( 'location_model' );
$this->load->model ( 'genre_model' );
$this->load->model ( 'category_model' );
$this->load->model ( 'booked_slot_model' );
$this->load->model ( 'cart_model' );
$this->load->model ( 'sp_model' );
$this->load->model ( 'updated_bookings_model' );

$this->user = $this->user_lib->get_user ();

$method_called = strtolower( $this->router->fetch_method() );
if ( $method_called != 'modify' && ! $this->user ) {
redirect ( site_url ( '/' ) );
}
}

public function index( $is_ajax = false ) {
$booked_slot_where = array (
'custId' => $this->user->get_id (),
'bsStatus' => 'active'
);
$booked_slots = $this->booked_slot_model->get_booked_slots ( $booked_slot_where );

$modified_slots = array ();
if (count ( $booked_slots ) > 0) {
foreach ( $booked_slots as $bs ) {
if ($bs->is_modifiable ()) {
$modified_slots [] = $bs;
}
}
}

$data = array (
'modified_slots' => $modified_slots,
'user' => $this->user,
'locations' => $this->location_model->get_all_locations (),
'genres' => $this->genre_model->get_all_genres (),
'categories' => $this->category_model->get_all_categories ()
);

if ($is_ajax) {
return $this->load->view ( 'booking/modify', $data, true );
} else {
$this->load->view ( 'booking/orders', $data );
}
}
}

 

The example here consist of two parts:

 

  1. Constructor: It initializes the models which will be required in this controller.
  2. Index: In CodeIgniter, a controller’s index function is responsible for its execution when you load the page with that URL. In the example, `http://domain.com/booking` will result in the execution of this function. For detailed tutorial on controller, please read CodeIgniter’s documentation. We will only concentrate on the code for this article.

 

Constructor Initialization

public function __construct() {
parent::__construct ();
$this->load->model ( 'booking_model' );
$this->load->model ( 'slot_model' );
$this->load->model ( 'location_model' );
$this->load->model ( 'genre_model' );
$this->load->model ( 'category_model' );
$this->load->model ( 'booked_slot_model' );
$this->load->model ( 'cart_model' );
$this->load->model ( 'sp_model' );
$this->load->model ( 'updated_bookings_model' );

$this->user = $this->user_lib->get_user ();
}

 

As mentioned above, this is a straightforward function to initialize all the models required in this controller.

 

Reusable Methods

public function index( $is_ajax = false ) {
$booked_slot_where = array (
'custId' => $this->user->get_id (),
'bsStatus' => 'active'
);
$booked_slots = $this->booked_slot_model->get_booked_slots ( $booked_slot_where );

$modified_slots = array ();
if (count ( $booked_slots ) > 0) {
foreach ( $booked_slots as $bs ) {
if ($bs->is_modifiable ()) {
$modified_slots [] = $bs;
}
}
}

$data = array (
'modified_slots' => $modified_slots,
'user' => $this->user,
'locations' => $this->location_model->get_all_locations (),
'genres' => $this->genre_model->get_all_genres (),
'categories' => $this->category_model->get_all_categories ()
);

if ($is_ajax) {
return $this->load->view ( 'booking/modify', $data, true );
} else {
$this->load->view ( 'booking/orders', $data );
}
}

 

In a nutshell, index method executes when the /booking uri is loaded in the address bar. Runs queries using models as we explained in part 2 of this series and passes the data to the view to be rendered. That’s it! The way it is achieved at Hirazi Webtech is a little different. We use DO layer to segregate our reusable logic. One such example being $bs->is_modifiable (). is_modifiable() method has the logic to return true/false based on whether that particular slot can be modified or not, which is defined in Slot object. The booking slot model returns the list of slot object basis the query. For reference, following is the is_modifiable method in the slot object:

 

public function is_modifiable() {
if( $this->get_status() !== 'active' ) {
return false;
}
$sp = $this->get_sp_obj();
$genre = self::$CI->genre_model->get_genre_by_id( $sp->get_genre_id() );
$slot = self::$CI->slot_model->get_slot_by_id( $this->get_slot_id() );

if( ! $slot ) {
return false;
}

$slot_time = $slot->get_date() . ' ' . $slot->get_from_time() . ' ' . $slot->get_am_pm() ;
$modify_allowed_time = $genre->get_allowed_modify_time();
$modify_time_limit = strtotime( '-' . $modify_allowed_time . 'hours', strtotime( $slot_time ) );

if( $modify_time_limit >= strtotime( date('Y-m-d H:i') ) ){
return true;
}

return false;
}

 

Conclusion

As it turns out, Controllers in CodeIgniter are majorly used as routers to guide the URI to an appropriate method which can then be executed. Most of the logic can be abstracted from the application itself using the DO layer.

Link to social platforms of Hirazi Webtech

We like to spread happiness. Let's do it together.

Contact details of Hirazi Webtech

Get in Touch

Would love to hear you over coffee!

Contact Detail

Hirazi Webtech

Mumbai, India

+91 96994-53388

contact.us@hirazi.com