Welcome Guest, Not a member yet? Register   Sign In
Accessing variables in controllers and views
#1

[eluser]xtremer360[/eluser]
Trying to figure out if this is the correct way to deal with the cms_template variable and if there's a simpler way I can do this. And if so how do do I properly access the cms_template in other controllers and also in views.

Code:
<?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');
  $this->data['cms_template'] = $this->cms_template;
}
}

#2

[eluser]Aken[/eluser]
Based on this example, $this->cms_template seems redundant and pointless, since all you're doing is calling a config item and then assigning it to a data array.
#3

[eluser]xtremer360[/eluser]
So your saying in every instance of $this->cms_template to $cms_template in that controller?
#4

[eluser]xtremer360[/eluser]
Because when I access the variable from another controller it gives me an error.
#5

[eluser]Fationably Late[/eluser]
I think Aken was trying to say that you are assigning $this->config->item() to an unnecessary variable. Since you are immediately passing that value into $this->data, then why don't you just do this:
Code:
$this->data['cms_template'] = $this->config->item('cms_template') ;
and bypass the variable?

But to answer your question, if the config item 'cms_template' is going to be global, or at least global in any controller that extends MY_Controller, why don't you put that assignment in the __construct of MY_Controller. Then you're keeping it DRY and you will have access to it (as well as $this->data) from any controller that extends it. But be sure to add a public $data to MY_Controller.

Code:
class MY_Controller extends CI_Controller {
    public $data;
    public function __contruct(){
        parent::__construct();
        $this->data['cms_template'] = $this->config->item('cms_template') ;
    }
}

Also, you'll never have to redeclare $data again either.
#6

[eluser]xtremer360[/eluser]
Okay sounds good. I also have my application set up like this:

/system
/application
/application/controllers/frontend/
/application/controllers/backend/

I have a login controller that's located in the backend subfolder and trying to figure out a how to access the controller as well as access the cms template variable for use in the login controller?
#7

[eluser]Fationably Late[/eluser]
I personally wouldn't set up your file structure that way, only because of the way CI handles URI's (yours would look like this: yoursite.com/backend/login ), but you probably have some clever routing going on that I don't see.

How separated are you trying to make the back end and front end? Are you wanting to effectively make them separate applications?
#8

[eluser]xtremer360[/eluser]
It might just be that I'm trying to do too much with this. Basically the effect I'm trying to go with is separate the actual site from my backend cms.
#9

[eluser]xtremer360[/eluser]
Any ideas on what I really should be doing with the purpose of this project?
#10

[eluser]Fationably Late[/eluser]
I can't really tell you what the purpose of your project is. But if it advice how to structure your files, then I can only give you a few suggestions to get your started. First, you want to think of your directories and/or in one of two ways:

1) Think of them as what an entity can do: application/controllers/admin/create_user
-OR
2) think of them as what can be done on an entity: application/controllers/user/create

I suggest these approaches because of the 'friendly URL'. But don't try to mix the two or else you're likely going to confuse yourself or your site users in the long run. And there are certainly tons more patterns out there.

If you really want to separate the cms from the user side, then you should do some research on putting the system folder in a common place and having two completely separate application folders.

I'm trying not to give you direct answers, but merely patterns that people use, because if you don't figure this stuff on your own, you won't understand why you're doing it.




Theme © iAndrew 2016 - Forum software by © MyBB