Welcome Guest, Not a member yet? Register   Sign In
Passing Variables from one controller to its extended controller
#1

[eluser]xtremer360[/eluser]


I'm trying to figure out how to handle a situation where I have a MY_Controller, Backend_Controller and of course the Backend extends the MY and I have other controllers like Login and other smaller controllers that extend the Backend. My question is if you notice on the login controller it needs to be able to access a variable inside of the backend controller? How can I get that variable to be used in this case?

In the backend controller I have this:

Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller
{
    function __construct ()
    {
        parent::__construct();

        $this->load->library('session');

        $cms_template = $this->config->item('cms_template');

        $this->data['template'] = $cms_template;
    }
}

Here's the login controller:

Code:
<?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Backend_Controller
{
    public function __construct()
    {
        parent::__construct();  
    }    

    public function index()
    {  
        $js_page_addons = '[removed][removed]';

        $page_view = 'login_view';

        $this->data['js_page_addons'] = $js_page_addons;
        $this->data['page_view'] = $page_view;
        $this->load->view('cms/' . $cms_template . '/usermanagement/index_view', $this->data);
    }
}

#2

[eluser]Rowan Wilson[/eluser]
There are ways to do this either by passing data via the URI, in session data or even by storing the data in the db.

However, rather than repeating stuff in both controllers, why not use your core MY_Controller to set the variables and then have both Controllers extend your MY_Controller. That way it keeps things DRY. Your already using it, why not make good use of it.

Just a thought.
#3

[eluser]xtremer360[/eluser]
So I can define the variable in the backed controller and use that variable in my login controller. I don't have to redefine it?
#4

[eluser]LuckyFella73[/eluser]
Yes you can do that.

Code:
<?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller
{
var $variable_used_by_login_too = 'value from backend_controller';

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

        $this->load->library('session');

        $cms_template = $this->config->item('cms_template');

        $this->data['template'] = $cms_template;
    }

// in case you want to alter the value depending on the controller that extends this one
function set_variable_used_by_login_too($data)
{
  $this->variable_used_by_login_too = $data;
}
}





// Login Controller
if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Login extends Backend_Controller
{
    public function __construct()
    {
        parent::__construct();
    }

    public function index()
    {
        $js_page_addons = '[removed][removed]';

        $page_view = 'login_view';

        $this->data['js_page_addons'] = $js_page_addons;
        $this->data['page_view'] = $page_view;
        $this->load->view('cms/' . $cms_template . '/usermanagement/index_view', $this->data);
    }

public function test()
{
  echo "Actual value of variable/ Property set in backend_controller: ".$this->variable_used_by_login_too;

  $this->set_variable_used_by_login_too('overwritten value from backend_controller');

  echo "Actual value of variable/ Property set in backend_controller: ".$this->variable_used_by_login_too;
}
}
#5

[eluser]xtremer360[/eluser]
I have the following and its not giving me an error anywhere. I am working locally. I have a bet that its not getting tht cms template variable in the login controller but I don't know. I had to remove the <> characters so it'll show that line of code.
Code:
&lt;?php
if (! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller
{  
    function __construct ()
    {
        parent::__construct();
    
        $this->load->library('session');
        
        $cms_template = $this->config->item('cms_template');
        
        $this->data['cms_template'] = $cms_template;
    }
}

Code:
&lt;?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kowmgr_login extends Backend_Controller {

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

public function index()
{
  $message_box_messages = array();
  $css_page_addons = '';
  $js_page_addons = 'script src='assets/' .$cms_template. '/js/validation/login_form.js/script';
  $meta_tag_addons = '';
  $site_title = 'KOW Manager Login';
  
  $page_view = 'login_view';
  
  if (count($message_box_messages) !== 0)
  {
   $message_boxes = $this->functions_model->build_message_boxes_output(array('display' => 'show', 'message' => $message_box_messages));
  }
  else
  {
   $message_boxes = array('display' => 'none');
  }
  
  if (isset($site_title) && (empty($site_title)))
  {
   $site_title = $this->functions_model->site_title();
  }
  
  $this->data['message_boxes'] = $message_boxes;
  $this->data['css_page_addons'] = $css_page_addons;
  $this->data['js_page_addons'] = $js_page_addons;
  $this->data['site_title'] = $site_title;
  $this->data['page_view'] = $page_view;
  
  $this->load->view('cms/' . $cms_template . '/usermanagement/index_view', $this->data);
}
}
#6

[eluser]xtremer360[/eluser]
I figured out why it wasn't working. It was because I was putting the frontend and backend controllers in the libraries folder instead of the core folder. I do have one more issue at hand. I have a configuration file called defaults.php located in the config folder and I have this config file autoloaded properly. Inside of my backend controller I define a variable called $cms_template and make the value of the variable a config file item value. If I then load the login controller it gives me a 500 error but if I make the value of the cms template variable in the backend controller have the value of 'supr' then it loads the login controller fine? Why is that? Why can't I define the variable as the config item.

Backend Controller
Code:
&lt;?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller
{
    protected $cms_template = $this->config->item('cms_template');

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

Login Controller
Code:
&lt;?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Kowmgr_login extends Backend_Controller {

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

public function index()
{
  echo $this->cms_template;
}
}
#7

[eluser]LuckyFella73[/eluser]
The use of $this-> is only possible from within an object context.
Try this way:

Code:
&lt;?php

if ( ! defined('BASEPATH')) exit('No direct script access allowed');

class Backend_Controller extends MY_Controller
{
    protected $cms_template = '';

    public function __construct()
    {
        parent::__construct();
$this->cms_template = $this->config->item('cms_template');
    }
}
#8

[eluser]xtremer360[/eluser]
Works great thank you.
#9

[eluser]xtremer360[/eluser]
How would I access the variable in a view because $this->cms_template does not work?




Theme © iAndrew 2016 - Forum software by © MyBB