views:

64

answers:

2

Hello

I'm fairly new to Zend Framework and MVC in general so I'm looking for some advice. We have a base controller class in which we have some methods to obtain some user information, account configurations, etc.

So I'm using some of those methods to write out code in various controllers actions, but now I want to avoid duplicating this code and further more I would like to take this code outside of the controller and in a view helper as it is mainly to output some JavaScript. So the code in the controller would look like this:

$obj= new SomeModel ( $this->_getModelConfig () );
$states = $obj->fetchByUser ( $this->user->getId() );
//Fair amount of logic here using this result to prepare some javascript that should be sent to the view...

The $this->_getModelConfig and $this->user->getId() are things that I could do in the controller, now my question is what is the best way to pass that information to the view helper once i move this code out of the controller ?

Should I just call these methods in the controller and store the results into the view and have the helper pick it up from there ?

Another option I was thinking of was to add some parameters to the helper and if the parameters are passed then I store them in properties of the helper and return, and when called without passing the parameters it performs the work. So it would look like this:

From controller:

$this->view->myHelper($this->user->getId(), $this->_getModelConfig());

From view:

<?= $this->myHelper(); %>

Helper:

class Zend_View_Helper_MyHelper extends Zend_View_Helper_Abstract
{
    public $userId = '';
    public $config = null;
    public function myHelper ($userId = null, $config = null)
    {
        if ($userId) {
            $this->userId = $userId;
            $this->config = $config;
        } else {
            //do the work
            $obj = new SomeModel($this->config);
            $states = $obj->fetchByUser($this->userId);
            //do the work here  
        }
        return $this;
    }
}

Any advice is welcomed!

A: 

Firstly the ASP style ending tag here at "$this->myHelper(); %>" is bad practice, with that said it is more advisable to keep the logic in the model and the controller just being used to call the model, get the results and spit that to the view for viewing.

what I would do is, if i simply want to pass a bunch of values to the view, i stuff them in an associative array and send them over.

anyway you should not be doing your ...

"//Fair amount of logic here using this result to prepare some javascript that should be sent to the view..."

part in the controller, I would advice you to make a new model that does that logic stuff for you, and you just call your model in the controller pass it what ever arguments that are needed and then spit the result of that to the view.

Akay
My question is exactly to take that code out of the controller. The thing I don't want to do is have my model output <script> tags and include JS files it does not belong there, so I'm thinking the solution is to use a helper. That 'fair amount of logic' is mostly just a few loops in the arrays received from the model and output the proper javascript, I don't really feel like this belongs in the model does it ?
SBUJOLD
A: 

The best way is to get the data from your model throught your controller, and then pass to the view. But if you really need a custom helper to echo the view parts, we only will know if you say exactly what you're trying to do.

If you already have this logic in a helper, try to just pass the parameters in your view myhelper($this->params); ?>

You may want take a look at this approach too:

// In your view to put javascript in the header
// You can loop trought your data and then use it to generate the javascript.
<?php $this->headScript()->captureStart(); ?>
    $().ready(function(){
        $('#slideshow').cycle({ 
            fx:     'fade', 
            speed:  1000, 
            timeout: 6500, 
            pager:  '#nav'
        });
    });
<?php $this->headScript()->captureEnd() ?>  
Keyne