• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Best way to pass function from MY_Controller to Controller

#1
Hello I would like to know what the best way is to pass a function from MY_Controller to A Controller?

PHP Code:
<?php

class MY_Controller extends CI_Controller {

    protected 
$data = array();

    public function 
__construct() {
        
parent::__construct();
        
$this->load->model('design/model_layout');
        
$this->load->model('extension/model_module');

        
$this->column_left();
        
$this->column_right();
        
$this->content_top();
        
$this->content_bottom();
        
$this->footer();
        
$this->header();
    }

    public function 
content_top() {
        
    }



So I can do this on multiple controllers

PHP Code:
<?php

class Welcome extends MY_Controller {

    
//public $data = array();

    
public function __construct() {
        
parent::__construct();
        
$this->load->library('position');
    }

    public function 
index() {
        
$data['column_left'] = $this->column_left();
        
$data['column_right'] = $this->column_right();
        
$data['content_top'] = $this->content_top();
        
$data['content_bottom'] = $this->content_bottom();
        
$data['footer'] = $this->footer();
        
$data['header'] = $this->header();


        
$this->load->view('home'$data);
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#2
Since Welcome controller extends MY_Controller, the methods are available as part of the Welcome controller. Just call it like any other method.

In your example, it looks like you want to have the content_top() method overridden by the Welcome controller, so you would simply make a new method with the same name in the Welcome controller. You might look at making the content_top method an abstract method in MY_Controller, if all controllers should have that method.
Reply

#3
(01-27-2016, 02:25 PM)kilishan Wrote: Since Welcome controller extends MY_Controller, the methods are available as part of the Welcome controller. Just call it like any other method.

In your example, it looks like you want to have the content_top() method overridden by the Welcome controller, so you would simply make a new method with the same name in the Welcome controller. You might look at making the content_top method an abstract method in MY_Controller, if all controllers should have that method.

I have though about this and is this more better if I create a render view in MY_Controller

PHP Code:
<?php

class MY_Controller extends CI_Controller {

    protected 
$data = array();

    public function 
__construct() {
        
parent::__construct();
        
$this->load->model('design/model_layout');
        
$this->load->model('extension/model_module');
    }

    public function 
_render_view($page '') {
        
$this->data['column_left'] = $this->column_left();
        
$this->data['column_right'] = $this->column_right();
        
$this->data['content_top'] = $this->content_top();
        
$this->data['content_bottom'] = $this->content_bottom();

        if (
file_exists(APPPATH 'views/theme/'$this->config->item('theme') .'/template/'$page '.php')) {
            
$this->load->view('theme/'$this->config->item('theme') .'/template/common/header'$this->data);
            
$this->load->view('theme/'$this->config->item('theme') .'/template/' $page$this->data);
            
$this->load->view('theme/'$this->config->item('theme') .'/template/common/footer' $page$this->data);
        } else {
            
$this->load->view('theme/default/template/common/header'$this->data);
            
$this->load->view('theme/default/template/'$page$this->data);
            
$this->load->view('theme/default/template/common/footer'$this->data);
        }
    }



And then

PHP Code:
<?php

class Welcome extends MY_Controller {

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

    public function 
index() {
        
$this->data['title'] = 'Home Page';
        
$this->_render_view('common/home');
    }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#4
That looks like a pretty good solution. I have done similar many times, but will load the current view into a variable. Then, I pass that, along with any other $data, to a main layout view. That keeps the theme as mostly a view and easier for some teams to edit.
Reply

#5
(01-27-2016, 11:09 PM)kilishan Wrote: That looks like a pretty good solution. I have done similar many times, but will load the current view into a variable. Then, I pass that, along with any other $data, to a main layout view. That keeps the theme as mostly a view and easier for some teams to edit.

Is it possible to load my widget controllers in to MY_Controller.

Because I have a in my APPPATH . 'controllers/widgets/Some_widget_name.php'

I would like some how to be able to do this I am what to use to load widget controllers. The code below is on my content_top function

PHP Code:
foreach ($modules as $module) {
 
 $part explode('.'$module['code']);

 
 if (isset($part[1])) {
 
   $setting_info $this->model_module->get_module($part[1]);

 
    if ($setting_info && $setting_info['status']) {
 
      $this->data['modules'][] = $this->load->widget('widget/some_name'$setting_info);
 
    }

 
  }

There's only one rule - please don't tell anyone to go and read the manual.  Sometimes the manual just SUCKS!
Reply

#6
(01-27-2016, 10:41 PM)wolfgang1983 Wrote:
(01-27-2016, 02:25 PM)kilishan Wrote: Since Welcome controller extends MY_Controller, the methods are available as part of the Welcome controller. Just call it like any other method.

In your example, it looks like you want to have the content_top() method overridden by the Welcome controller, so you would simply make a new method with the same name in the Welcome controller. You might look at making the content_top method an abstract method in MY_Controller, if all controllers should have that method.

I have though about this and is this more better if I create a render view in MY_Controller

PHP Code:
<?php

class MY_Controller extends CI_Controller {

 protected 
$data = array();

 public function 
__construct() {
 
parent::__construct();
 
$this->load->model('design/model_layout');
 
$this->load->model('extension/model_module');
 }

 public function 
_render_view($page '') {
 
$this->data['column_left'] = $this->column_left();
 
$this->data['column_right'] = $this->column_right();
 
$this->data['content_top'] = $this->content_top();
 
$this->data['content_bottom'] = $this->content_bottom();

 if (
file_exists(APPPATH 'views/theme/'$this->config->item('theme') .'/template/'$page '.php')) {
 
$this->load->view('theme/'$this->config->item('theme') .'/template/common/header'$this->data);
 
$this->load->view('theme/'$this->config->item('theme') .'/template/' $page$this->data);
 
$this->load->view('theme/'$this->config->item('theme') .'/template/common/footer' $page$this->data);
 } else {
 
$this->load->view('theme/default/template/common/header'$this->data);
 
$this->load->view('theme/default/template/'$page$this->data);
 
$this->load->view('theme/default/template/common/footer'$this->data);
 }
 }



And then

PHP Code:
<?php

class Welcome extends MY_Controller {

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

 public function 
index() {
 
$this->data['title'] = 'Home Page';
 
$this->_render_view('common/home');
 }

hello this is issue what i have been looking for , then i make documentation is here http://www.sidaurukfreddy.com/2016/01/lo...oller.html


but my code is really nasty, need some digging on it ?
anyone make blog for clean code load variable for all controller, thanks
Reply


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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