CodeIgniter Forums

Full Version: Adding property in all Contrellers: MY_Controller vs BaseController
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
I would like each controller in my app to have a property called $controller_url that I would set to

$this->config->item('base_url') . $this->uri->segment(1);

In this way in each controller I will always have the controller url ready to be passed to views.



What's the best practice to do this?

Should I add an application/core/MY_Controller.php

Code:
class MY_Controller extends CI_Controller
{  
   protected $controller_url;
  
   function __construct() {
      parent::__construct();      
      $this->controller_url = config_item('base_url') . $this->uri->segment(1);      
   }

}

, or should I simply create a sort of base controller and extends all my controllers from that one i.e.

application/controllers/basecontroller.php

Code:
class BaseController extends CI_Controller
{  
   protected $controller_url;
  
   function __construct() {
      parent::__construct();
      $this->controller_url = config_item('base_url') . $this->uri->segment(1);      
   }

}

class Invoices extends BaseController
{  
   function __construct() {
      parent::__construct();
   }

   function listInvoices() {
      $vars['year'] = $year;
      $vars['controller_url'] = $this->controller_url;
      ...
      $this->load->view('invoices_v.htm', $vars);
}

What do you think?
It's a matter of personal preference. The one possible reason to go with the BaseController is you can still extend CI_Controller without bringing the $controller_url property along if needed.

About the code. You have one syntax error - you're failing to set the class property in the constructor and are setting a local variable instead.

PHP Code:
$controller_url config_item('base_url') . $this->uri->segment(1); 

should be
PHP Code:
$this->controller_url config_item('base_url') . $this->uri->segment(1); 

In case you're interested there are other ways to get the controller.
Here's one way that's a tiny bit more efficient than calling $this->uri->segment(1)
PHP Code:
$this->controller_url config_item('base_url') . $this->router->class

Or, you could use the PHP magic constant __CLASS__
PHP Code:
$this->controller_url config_item('base_url' __CLASS__

Or a not so efficient but probably more robust
PHP Code:
$this->controller_url $this->config->base_url($this->router->class); 
Thanks, I fixed the code in OP.

(12-06-2018, 08:13 AM)dave friend Wrote: [ -> ]In case you're interested there are other ways to get the controller.
Here's one way that's a tiny bit more efficient than calling $this->uri->segment(1)
PHP Code:
$this->controller_url config_item('base_url') . $this->router->class

Thanks, I'm going to look into this one. I stil have not played with the router, I'm a novice.  Big Grin

(12-06-2018, 08:13 AM)dave friend Wrote: [ -> ]Or, you could use the PHP magic constant __CLASS__
PHP Code:
$this->controller_url config_item('base_url' __CLASS__

Good point, maybe I could even use

PHP Code:
$this->controller_url config_item('base_url' . static::class; 

in case I extend the controller, the late static binding would do the job and I wouldn't have to declare again in the extended class $this->controller_url = config_item('base_url')  . __CLASS__;  Wink
(12-06-2018, 08:52 AM)Balenus Wrote: [ -> ]Good point, maybe I could even use

PHP Code:
$this->controller_url config_item('base_url' . static::class; 

in case I extend the controller, the late static binding would do the job and I wouldn't have to declare again in the extended class $this->controller_url = config_item('base_url')  . __CLASS__;  Wink

I use $this->router->class to avoid the confusion that the inheritance chain might cause and eliminate the need to redeclare in extended classes.  
Using $this->router->class should also by-pass problems that custom routes might introduce.