• 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Setting default variables in a controller

#1
[eluser]deineMudder[/eluser]
Hello i am still a newbie to CI and have a basic question again.

I got my Controller User:

Code:
<?php
class User extends Controller {
function User()
{
  parent::Controller();
  if ($this->session->userdata('logged_in') != TRUE){
   redirect('/admin/login/','location');
  }
}

  function activation()
  {
   $controller = 'user';
   $data['page_vars'] = $this->libadmin->page_vars($controller);
   ...
  }

  function add()
  {
   $controller = 'user';
   $data['page_vars'] = $this->libadmin->page_vars($controller);
   ...
  }

  function search()
  {
   $controller = 'user';
   $data['page_vars'] = $this->libadmin->page_vars($controller);
   ...
  }
....
...
..
.


In order to define some variables im passing the controller to a library to return the variables i want. Now i want to reduce my code a bit and was thinking that i could put the 2 first lines of each function in the controller somewhere into a global variable but it seems im unable to do it Sad

i wanted it to look like this:
Code:
<?php
class User extends Controller {
function User()
{
  parent::Controller();
  
  $controller = 'user';
  $data['page_vars'] = $this->libadmin->page_vars($controller);  

  if ($this->session->userdata('logged_in') != TRUE){
   redirect('/admin/login/','location');
  }  
}

function activation()
  {  
  i wanna be able to access $controller here....
  }


anyone got an idea on how to solve it?

#2
[eluser]WeeJames[/eluser]
Code:
<?php
class User extends Controller {

var $controller;

function User()
{
  parent::Controller();
  
  $this->controller = 'user';
  $data['page_vars'] = $this->libadmin->page_vars($this->controller);  

  if ($this->session->userdata('logged_in') != TRUE){
   redirect('/admin/login/','location');
  }  
}

function activation()
  {  
  //access the global var with $this->controller anywhere you want in this class
  }

Try that.. i assume its just the string 'user' that you're trying to access.

You could also place the page_vars data into a variable in the same way.

Code:
<?php
class User extends Controller {

var $page_vars;

function User()
{
  parent::Controller();
  
  $this->page_vars = $this->libadmin->page_vars('user');  

  if ($this->session->userdata('logged_in') != TRUE){
   redirect('/admin/login/','location');
  }  
}

function activation()
  {  
  //now the page_vars data is available throughout this class via $this->page_vars and is always available after construction
  }

Read up on classes in php as this type of stuff isn't really CI related.

#3
[eluser]deineMudder[/eluser]
works fine for the controller variable, ty alot.
could u give me a hint how it works with the array too?
i get some nasty errors now destroying my whole page Wink

#4
[eluser]WeeJames[/eluser]
Without seeing how you're using it i wouldn't be able to figure out whats going wrong Smile

#5
[eluser]deineMudder[/eluser]
mmhhh
basically my "library" looks like this

Code:
class LibAdmin {

function page_vars($controller)
{
switch ($controller){
  case "login":
   $page_vars['db_table'] = 'user';
   $page_vars['title']     = ":: Login";
   $page_vars['heading'] = "Login";
   $page_vars['view'] = "content_login.php";
   $page_vars['metatags'] = array("");
   $page_vars['nav_elements'] = array('extra');
  break;

  case "user":
   $page_vars['db_table'] = 'user';
   $page_vars['title'] = ":: Userverwaltung";
   $page_vars['heading'] = "User";
   $page_vars['view'] = "content_user.php";
   $page_vars['metatags'] = array("");
   $page_vars['nav_elements'] = array('search','add','detail','edit','delete');
  break;
}

$page_vars['timestamp'] = time();
$page_vars['time'] = mdate('%Y-%m-%d %H:%i:%s',$page_vars['timestamp']);
$page_vars['controller'] = $controller;

return ($page_vars);
}

and im using those variables in my functions of the controller and views and so on...

#6
[eluser]Glen Swinfield[/eluser]
Just keep using $this->

for example:

Code:
function whatever{
   $this->page_vars = array(); // now assigned as available throughout the class
   //Then
   $this->page_vars['keyname'] = 'mystring';
}

Then access the values like this:

Code:
echo $this->page_vars['keyname']; //prints 'mystring';

It's all about $this when you're using objects - check the php manual for more info.

#7
[eluser]deineMudder[/eluser]
okay ty and sorry again for asking such a question,
i am not used to OO scripting.

thx alot for ur answers guys

#8
[eluser]WeeJames[/eluser]
If you're placing the page_vars array in $this->page_vars, the values should be accessible via $this->page_vars['view'] etc. If you're passing the whole array into your view.. e.g. $this->load->view('view', $this->page_vars) they'll be accessible via $heading and similar..


Digg   Delicious   Reddit   Facebook   Twitter   StumbleUpon  


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