开发者

MVC Optimization question Framework Zend

Just a quick logical question.

I have 2 Zend Controllers namely Merchant and Account. Merchant Controller has an action called editAction. My question is whether it is possible to have same action in Account as well without duplicating the code. I managed to create a include file for the view but I like to have a best way to manage my code. I currently have an idea of having an Helper class and invoke that helper class to both these Controllers. But I kno开发者_StackOverfloww someone would have got better solution than this. Please help me.

Thank you


The simplest solution would be to extend Zend_Controller_Action into your own base class and put editAction() into that. For example, assuming you have model classes named 'Account' and 'Merchant':

abstract class My_Controller_Action extends Zend_Controller_Action
{
    protected $_modelName;

    public function editAction()
    {
        $model = new $this->_modelName();
        // Do your editing here.
    }
}

class AccountController extends My_Controller_Action
{
    protected $_modelName = 'Account';
}

class MerchantController extends My_Controller_Action
{
    protected $_modelName = 'Merchant';
}

Keep in mind that this design implies that the code in editAction() would work for both Accounts and Merchants.


I think the best course of action would be to put majority of the logic inside your Models and keep your controllers lean. Your models can extend your own class which would have common operations in it.

You could also write your own class, it doesn't have to be a helper, and use it in the controllers to save the entity:

$saver = new My_Editing_Class();
$saver->edit("account",$this->getRequest()->getPost()); // Editing "account" with the POST data.

You then do your magic inside "My_Editing_Class".

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜