Welcome Guest, Not a member yet? Register   Sign In
loaded model available in view without passing it explicitly from controller
#1

Hello dear forum members,

Today I suddenly notices a - for me - strange behavior in CI 2. Hopefully I find some insight here of whether this a bug is or not..?

When I load a self made test model in a controller and DON'T pass this to a view, this model is still available in this particular view. This to my amazement I may add! Because I thought this should not be possible.

This is the situation:

Model
PHP Code:
class Test_model extends CI_Model {

 
   function __construct()
 
   {
 
       parent::__construct();
 
   }

 
   function getData()
 
   {
 
       $query $this->db->get('products');
 
       return $query;
 
   }


Controller
PHP Code:
class Welcome extends CI_Controller
{
    function 
__construct()
    {
        
parent::__construct();

        
$this->load->helper('url');
        
$this->load->library('tank_auth');
    }

    

 
       function test()
 
       {
 
           $this->load->model('test_model');
 
           
            $this
->load->view('welcome_message');
 
       }



View
PHP Code:
<!DOCTYPE html>
<
html lang="en">
<
head>
    <
meta charset="utf-8">
    <
title>Welcome to CodeIgniter</title>
</
head>
<
body>


<
ul>
 
   <?php
    foreach 
($this->test_model->getData()->result() as $product) {
 
       echo "<li>";
 
       print_r($product);
 
       echo "</li>";
 
   }
</
ul>
?>

</body>
</html> 

Output
The output of this is a page showing a list with all the product details per list-item. 

Conclusion and question
The model was not passed to the view in an array as second parameter in the controller ( like: $this->load->view('path/to/view', $data); ), but still is available in the view through "$this->model_name". 
Is this intentionally?
It is my understanding that the query in the model should be passed as a variable in the controller to the view. So that the view cannot get to perhaps sensitive data in the model. The availabillity of the model data should be controlled in the controller! Or do overlook something..?
Reply


Messages In This Thread
loaded model available in view without passing it explicitly from controller - by Ametad - 11-12-2015, 08:14 AM



Theme © iAndrew 2016 - Forum software by © MyBB