CodeIgniter Forums

Full Version: Passing controller object into view (Is this bad?)
You're currently viewing a stripped down version of our content. View the full version with proper formatting.

El Forum

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)
function index()

manage view snippet
<?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.

El Forum

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.

El Forum

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.

<?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')));
<div id="feedback_bar"></div>
<div id="table_action_header">
        <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;
<div id="table_holder">
&lt;?php echo $manage_table; ?&gt;
&lt;?php $this->load->view("partial/footer"); ?&gt;

El Forum

[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.

El Forum

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