What happened to the construct() and what is intController()?!


Posted by Tuts Admin on 2018-07-30

Contributions

 

Want to be a contributing author and help grow the CodeIgniter community?

Register and account and use the contact form and drop us a note.

Donations

 

We appreciate any donation you're willing to send.

Visit the donations page

Donations collected are used to pay for hosting, domain names, further development of the website, and gives me a little more time to write tutorials.

Subscribe

Get an email when new content is added.


0 v4-dev Controllers News

Most of use are used to using __construct() in our Controllers to set up things and add our own setup, and there's been some confusion around the disappearance of the requirement to construct the parent in CodeIgniter 4. This really isn't the end of the world.

This is what we're all used to doing in CodeIgniter 3. It's just no longer needed in CodeIgniter 4.

public function __construct()
{
  parent::__construct();
} 

So, what happened?

Well, nothing really. Kilishan (The lead CI4 developer) has introduced initController(), which basically does the parent construct loading for us, we as developers, just don't need to worry about it anymore.

Yay! Right?

well, yeah, pretty much.

You no longer need to construct the parent to get the parent's functionality so unless you need/want to set up class-wide resources, you don't need to add __construct() at all.

Let's say you do have a resource that all, or most, methods in your Controller will use and you're all about K.I.S.S.(Keep It Simple Stupid) or maybe you really like DRY (Don't Repeat Yourself) So what's the new way look like? Surprisingly similar to the old way.

A complete example with a database and builder built in just for fun in application/Controllers/Mycontroller.php and we'll use the __construct() to make these available to every method in the class.

<?php namespace App\Controllers;

use CodeIgniter\Controller;

class Mycontroller extends Controller {

  // property for our database connection : accessed as $this->db
  public $db;

  // property for our query builder : accessed as $this->builder
  public $builder;

  // set the table : accessed as $this->table
  protected $table = 'mytable';


  public function __construct()
  {
     log_message('debug', 'Mycontroller Class Initialized');

    // instantiate the database connection
    $this->db = \Config\Database::connect();

   // set the table and create the query builder instance
   $this->builder = $this->db->table($this->table);

}

Now we have access to $this->builder and $this->db for every method in your controller.

    public function page($id)
    {
       // because we've already defind the table, all we have to do now is `where()` and  'get()' our records
       $q = $this->builder->where('id', $id)->get();

      // get the result object and return it.
      $data['pageData'] =  $q->getRow();   

     // output the view 
      echo view('MyView', $data);
    }

}

And, that's it.

Thanks to Kilishan for his expertise.

References

Thanks for reading and see you next time!