CodeIgniter Forums
Passing controller object into view (Is this bad?) - Printable Version

+- CodeIgniter Forums (https://forum.codeigniter.com)
+-- Forum: Archived Discussions (https://forum.codeigniter.com/forum-20.html)
+--- Forum: Archived Development & Programming (https://forum.codeigniter.com/forum-23.html)
+--- Thread: Passing controller object into view (Is this bad?) (/thread-12409.html)



Passing controller object into view (Is this bad?) - El Forum - 10-17-2008

[eluser]blasto333[/eluser]
I have a view that is used by two controllers both which implement a common interface.



In the controllers here is how I load the view.

Customers controller (implements iPersonController)
Code:
function index()
{
    $data['controller_name']=strtolower(get_class());
    $data['controller']=$this;
    $data['manage_table']=get_people_manage_table($this->Customer->get_all(),$this);
    $this->load->view('people/manage',$data);
}


manage view snippet
Code:
<?php echo anchor("$controller_name/view/-1/width:".$controller->_get_form_width().'/height:'.$controller->_get_form_height(),"New");
?>


Is it a bad practice to send in a reference to a controller, when I have a legit reason to be doing so.
($controller->_get_form_width())


Passing controller object into view (Is this bad?) - El Forum - 10-18-2008

[eluser]Bramme[/eluser]
Why not generate your anchor in your controller and pas that html to your view?

I'm not sure it's really "bad practice", I've just found that there's most of the times some way of working around it.


Passing controller object into view (Is this bad?) - El Forum - 10-18-2008

[eluser]blasto333[/eluser]
My view is kind of complicated, it just seems to make more sense to only pass in a couple of things.


Here is a simplified version of my view.

Code:
<?php $this->load->view("partial/header"); ?>
<div id="title_bar">
    <div id="title" class="float_left">&lt;?php echo $this->lang->line('common_list_of').' '.$this->lang->line('module_customers'); ?&gt;</div>
    <div id="new_button" class="float_right">
        &lt;?php echo anchor("$controller_name/view/-1/width:".$controller->_get_form_width().'/height:'.$controller->_get_form_height(),
        "<div class='big_button'><span>".$this->lang->line($controller_name.'_new')."</span></div>",
        array('class'=>'thickbox none','title'=>$this->lang->line($controller_name.'_new')));
        ?&gt;
    </div>
</div>
<div id="feedback_bar"></div>
<div id="table_action_header">
    <ul>
        <li class="float_left"><span>&lt;?php echo anchor("$controller_name/delete",$this->lang->line("common_delete"),array('id'=>'delete')); ?&gt;</a></span></li>
        <li class="float_left"><span><a href="#" id="email">&lt;?php echo $this->lang->line("common_email");?&gt;</a></span></li>
        <li class="float_right">
        <img src='&lt;?php echo base_url()?&gt;images/spinner_small.gif' alt='spinner' id='spinner' />
        &lt;?php echo form_open("$controller_name/search",array('id'=>'search_form')); ?&gt;
        &lt;input type="text" name ='search' id='search'/&gt;
        &lt;/form&gt;
        </li>
    </ul>
</div>
<div id="table_holder">
&lt;?php echo $manage_table; ?&gt;
</div>
&lt;?php $this->load->view("partial/footer"); ?&gt;



Passing controller object into view (Is this bad?) - El Forum - 10-18-2008

[eluser]Colin Williams[/eluser]
You aren't working under any laws, here, so I don't think on the surface anybody can really say it's right or it's wrong. In classic MVC, the View is constantly in contact with the Controller and sometimes the Model.

I, personally, would just pass the value of the form width along with the $data array. You should have a standard way of doing it. Either the Controller always passes data to the View or the View always actively requests the data it needs from the Controller.


Passing controller object into view (Is this bad?) - El Forum - 10-18-2008

[eluser]blasto333[/eluser]
I think you are right in this case, two more parameters is not painful and goes along with following a standard.