CodeIgniter Forums
Is there a way to pass every variable in a controller action, to the view automatical - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Using CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=5)
+--- Forum: Choosing CodeIgniter (https://forum.codeigniter.com/forumdisplay.php?fid=8)
+--- Thread: Is there a way to pass every variable in a controller action, to the view automatical (/showthread.php?tid=68260)



Is there a way to pass every variable in a controller action, to the view automatical - desbest - 06-16-2017

Is there a way to pass every variable in a controller action, to the view automatical

So instead of 

PHP Code:
<?php
class News extends CI_Controller {
 
       public function index()
 
       {
 
               $data['test'] = "hello";
 
       }

I can have


PHP Code:
<?php
class News extends CI_Controller {
       public function index()
       {
               $test = "hello";
       }




And I can echo $test in the view and the word "hello" appears as the variable is passed automatically to the view without me having to manually pass it myself.


RE: Is there a way to pass every variable in a controller action, to the view automatical - reactionstudio - 06-16-2017

this isn't quite how i do it but you could do something like:

PHP Code:
<?php
class News extends CI_Controller {
        
        private 
$data = array();

 
       public function index()
 
       {
 
               $this->data['stories'] = $this->News_story->get_list();
                
$this->load->view('common/wrapper',$this->data);
 
       }

 
       public function story($news_story_id)
 
       {
 
               $this->data['story'] = $this->News_story->get($news_story_id);
                
$this->load->view('common/wrapper',$this->data);
 
       }




RE: Is there a way to pass every variable in a controller action, to the view automatical - InsiteFX - 06-17-2017

PHP Code:
$data['test'] = 'test';

$this->load->vars($data)

// Notice you do not need to pass it now in the view
// It makes it global to all views.

$this->load->view('common/wrapper'); 



RE: Is there a way to pass every variable in a controller action, to the view automatical - dave friend - 06-17-2017

Automatically? No.


RE: Is there a way to pass every variable in a controller action, to the view automatical - Kaosweaver - 06-19-2017

Is this what you want to do:

http://php.net/manual/en/function.get-defined-vars.php
This function returns a multidimensional array containing a list of all defined variables, be them environment, server or user-defined variables, within the scope that get_defined_vars() is called.

Then you can pass that to the view (or, I dunno, make it a global variable and access it that way)

I don't recommend any of this, setting up a $data variable doesn't seem like a burden worth working around...