Welcome Guest, Not a member yet? Register   Sign In
Calling 'require_once' from a controller
#1

[eluser]thekrow[/eluser]
Hello,

I'd like to know why I'm getting an error when I call "load_file()" located in my index function in my one of my controllers, something like this:

function index()
{
$this->load_files();
}

function load_files()
{
require_once('top_file.php');

$this->smarty->assign('top',$top);
}

The error I'm getting is "Undefined variable: top". The 'top' variable as you can see from the code is stored in "top_file.php". I don't understand why the variable is undefined. If I call "require_once()" directly in index() then my program works fine. The problem is when I use require_once in a different function different from index().

Any help/suggestions will mean a lot to me, thank you.
#2

[eluser]Pert[/eluser]
require_once, as name suggest, will attach additional script only once, the first time it was called.

Have you tried using just include or require?
#3

[eluser]thekrow[/eluser]
Hi Pert,

Yes same error, I thought the same you said and that's why I tried include instead but I get the same :S

[quote author="Pert" date="1370420759"]require_once, as name suggest, will attach additional script only once, the first time it was called.

Have you tried using just include or require?[/quote]
#4

[eluser]Pert[/eluser]
Odd, I've definitely messed about with $this in script included in the middle of the class method.

Maybe try just setting the variable before include?

Code:
class xxx
{
   function yyy()
   {
      var $top;
      include 'other-file.php';
      ...
   }
}

#5

[eluser]thekrow[/eluser]
Hi Pert, maybe if I just paste my two functions you'll be able to understand this better. What happens is, I...

1. get my language from an array contained in one of my libraries (first one is the default language)
2. load content files
3. get variables from the files and assign them to be passed into my view
4. Display template based on input

If I copy/paste the content of load_files() into index() then it works. The only thing I did was to call a function instead of having all those include() calls in my index(). The reason I want to load them in a separate function is because I want to convert load_files() into a helper function, basically because I load the bunch of files (the ones I'm calling with 'include') in different controllers. It's better for me to just call load_files($language,$page) in my controllers and not having of that bunch of require functions every time I need them. I hope I'm clear.

Code:
function index($page = 'home')      
{    
  //not_found($page);        // Quit if page is not found

  $language = $this->multilanguage->language;  // Select either default or set new lang depending on dropdown choice
  
  // Load files
  $this->load_files($language,$page);  
  
  // Assign variables
  $this->smarty->assign('url',$this->multilanguage->url_to_be_assigned($page)); // URL assigned for switching language
  $this->smarty->assign('page',$page.'.tpl');    
  $this->smarty->assign('title',title($page));    // title() helper used
  $this->smarty->assign('meta',meta($meta[$page]));   // meta() helper passes array of meta tags based on input page  
  $this->smarty->assign('top',$top);      
  $this->smarty->assign('category',$category);  
  $this->smarty->assign('right_content',$right_content);
  $this->smarty->assign('footer',$footer);    
  $this->smarty->assign('data',$data);      // Main content
  
  ob_start();
   isset($_SESSION['poll_result']) ? $this->poll_result() : $this->poll_item();
   $this->smarty->assign('poll', ob_get_contents());
   unset($_SESSION['poll_result']);
  ob_end_clean();
  
  // Display template based on input
  $this->display_template($this->select_template($page));  

}

function load_files($language,$page)
{
  include APPPATH.'language/'.$language.'/'.str_replace('-','_',$page).'_lang.php';
  include APPPATH.'language/'.$language.'/includes_mid_content_lang.php';  
  include APPPATH.'language/'.$language.'/includes_top_lang.php';    
  include APPPATH.'language/'.$language.'/includes_right_content_lang.php';  
  include APPPATH.'language/'.$language.'/includes_footer_lang.php';    
  include APPPATH.'language/'.$this->multilanguage->select_meta_file($language);
}

[quote author="Pert" date="1370437947"]Odd, I've definitely messed about with $this in script included in the middle of the class method.

Maybe try just setting the variable before include?

Code:
class xxx
{
   function yyy()
   {
      var $top;
      include 'other-file.php';
      ...
   }
}

[/quote]
#6

[eluser]jairoh_[/eluser]
from what i know requiring a file pretty much suitable for views. it could be in the controller if will not show an error but not appropriate.
#7

[eluser]Pert[/eluser]
Ah, right, I can now see what's going on.

Your includes are included in your <strong>load_files</strong> method. Any local variables set in the method will be deleted once method has finished and are therefore not available.

Code:
function index()
{
    $this->load_files();
    echo $a; // $a is undefined
}

function load_files()
{
   // via include
   $a = 'a';
}

You can either attach your language elements to <b>$this</b> object in include file:

Code:
// before
$a = 'random language element';

// after
$this->a = 'random language element';

Or you can move includes in main method.
#8

[eluser]Pert[/eluser]
Or you could use <b>global</b> directive.

http://php.net/manual/en/language.variables.scope.php
#9

[eluser]thekrow[/eluser]
Pert,

Thanks a lot! I used the global definition and even though I'm still having a little issue with the data that's being echoed, your idea worked! Smile

Cheers

[quote author="Pert" date="1370593158"]Or you could use <b>global</b> directive.

http://php.net/manual/en/language.variables.scope.php[/quote]
#10

[eluser]Pert[/eluser]
[quote author="thekrow" date="1370620951"]Pert,

Thanks a lot! I used the global definition and even though I'm still having a little issue with the data that's being echoed, your idea worked! Smile

Cheers
[/quote]

Happy to help Smile




Theme © iAndrew 2016 - Forum software by © MyBB