Admin area - MY_Controller

Update at 2017-12-22 07:58:02


Introduction

MY_Controller in admin area placed at the admin/core/MY_Controller.php. We override CI_Controller of system codeigniter to define some scripts we need when app run as build layout and partial view, middleware, boot, register for your app...

Build layout and partial view

By default codeigniter use $this->load->view() to render view file. But in CI Base, we use $this->layout() to render view with the layout.

Why $this->layout() not $this->load->view()

$this->layout() will build view file as body of view and combine with partials (header, footer, column left) to the final view. It helpful when you render view of controller

Partial views available:

  1. Header by the header() method
  2. Column left by th column_left() method
  3. Footer by the footer() method

To get details please open admin/core/MY_Controller@layout

Middleware

In codeigniter, the request go straight to controller. But in CI Base, the request filtered by some middle components called middleware. Middleware will check the request before call controller destination. By default, we have some middlewares:

  1. Required authentication by required_authentication() method
  2. Redirect if authenticated by redirect_if_authenticated() method
  3. Redirect if turn off register function by redirect_if_off_register() method
  4. Check permission by policy() method
  5. Check token reset password by token_reset_password() method

Register hook

Sometimes we need to register some information or scripts when app running, example define permission, load language... To resolve issue, we defined a register hook in MY_Controller. If you need to register for your app, place your code there!

Note: Script in register hook is trigger first before any scripts or methods

Boot hook

Boot hook is contains common scripts when app is running.

Note: Boot hook trigger after register hook

Boot vs register

Boot and register hook have a similarity is "run first" (like constructor) but register only use to define, not logic here. Boot hook only use to logic, not define

<?php

protected function register()
{
    // code
}

protected function boot()
{
    // code
}