Welcome Guest, Not a member yet? Register   Sign In
Adding property in all Contrellers: MY_Controller vs BaseController
#1
Question 
(This post was last modified: 12-06-2018, 08:42 AM by Balenus. Edit Reason: code fix )

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?
Reply


Messages In This Thread
Adding property in all Contrellers: MY_Controller vs BaseController - by Balenus - 12-06-2018, 05:53 AM



Theme © iAndrew 2016 - Forum software by © MyBB