• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Accessing variables in controllers and views

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.


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

class Backend_Controller extends MY_Controller
    protected $cms_template = '';
public function __construct()
  $this->cms_template = $this->config->item('cms_template');
  $this->data['cms_template'] = $this->cms_template;

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.

So your saying in every instance of $this->cms_template to $cms_template in that controller?

Because when I access the variable from another controller it gives me an error.

[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:
$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.

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

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

Okay sounds good. I also have my application set up like this:


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?

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

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.

Any ideas on what I really should be doing with the purpose of this project?

[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
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.

Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  

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