• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
intController() and __construct()

#1
I'd like to make sure I have my head wrapped around the removal no longer need of __contruct() from controllers to construct the parent.


I I've got that 
PHP Code:
public function __construct()
{
 
  parent::__construct();


specifically is no longer needed as intController() takes care of all that.


So, where I'm a bit fuzzy is this:
When I need/want to initialize a controller wide access to a resource, IE: (shortened for brevity),
The "old way"

PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
MyController extends Controller
{

    
/**
    * The database object
    *
    * @var object
    */
    
protected $db;

    
/**
    * The query builder object
    *
    * @var object
    */
    
protected $builder;

 
       /**
    * The name of the table
    *
    * @var string
    */
    
public $table '';

    
// ------------------------------------------------------------------------

    /**
    * Setup all vars
    *
    * @param array $config
    * @return void
    */
    
public function __construct($config = array())
    {
        
// database connection
        
$this->db = \Config\Database::connect();
        
        
// set the table
        
$this->builder $this->db->table($this->table);
    }

 
       // etc...


Is this still valid to set up a controller-wide available resource?

or should we be using these in each method when we need them and not use __construct() at all?  IE:

PHP Code:
<?php namespace App\Controllers;

use 
CodeIgniter\Controller;

class 
MyController extends Controller
{

    
/**
    * The database object
    *
    * @var object
    */
    
protected $db;

    
/**
    * The query builder object
    *
    * @var object
    */
    
protected $builder;

 
       /**
    * The name of the table
    *
    * @var string
    */
    
public $table '';

    
// ------------------------------------------------------------------------

    
    
public function someMethod($someVar)
    {
        
// database connection
        
$this->db = \Config\Database::connect();
        
        
// set the table
        
$this->builder $this->db->table($this->table);

 
               $q $this->builder->select('someField')->where('someField'$someVar)->getResult();

 
               // do something with $q...
    
}

 
       // etc...


I realize using database and builder is open to 'the fat/skinny controller/model argument', this is just an example to ask my question...
Reply

#2
You're overthinking it Smile

Yes - it's perfectly valid to do controller-wide resources.

Just forget the initController() exists and you'll be fine. That's used by the framework, not by you. It just makes it a little simpler/cleaner when you want to create controller-wide resources. Instead of:

Code:
public function__construct(...$params)
{
    parent::__construct(...$params);

    // Your stuff goes here...
}

You would do:

Code:
public function__construct()
{
    // Your stuff goes here...
}
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply

#3
(07-30-2018, 03:13 PM)kilishan Wrote: You're overthinking it Smile
It's how I roll.   Big Grin

Thanks!
Reply

#4
(07-30-2018, 03:29 PM)enlivenapp Wrote:
(07-30-2018, 03:13 PM)kilishan Wrote: You're overthinking it Smile
It's how I roll.   Big Grin

Thanks!

I've been known to do that myself, that's how I can recognize it in you lol.
Support Development  • Practical CodeIgniter 3  • Vulcan - CLI Tools for CI4
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


Users browsing this thread:
1 Guest(s)


  Theme © 2014 iAndrew  
Powered By MyBB, © 2002-2018 MyBB Group.