HMVC in Zend Framework

二次信任 提交于 2020-01-02 05:39:07

问题


Is it possible to use the HMVC pattern in Zend Framework? It is implemented in Kohana 3 by default and I really love it, so now I want to use it in Zend Framework.

Edit

I want to make it possible to: 1) include a complete request (like controller/action) inside an other request 2) make a direct call to the controller/action as above

It is not only used for widgets, but I also want to build a page which contains content of other pages...

Edit 2

To be a bit more clear: I do have a page object that contains several elements. These elements can be simple elements (text, image, etc) and special elements, which are controller:action calls. Each page can contain "unlimited" (special) elements. I simply want to loop through these elements, define which kind of element I'm dealing with and add the result of that Element to the content of my view.

Like:

foreach($Page->Elements AS $Element) {
    switch(get_class($Element)) {
        case "Base\TextElement":
            // Add text element to content
            ...
            break;
    case "Base\SpecialElement":
            // Get result of the controller:action call
            break;
        case "Base\ImageElement":
            // Add image element to content
            ...
            break;
        default:
            echo "No case defined for ".get_class($Element);
            die;
    }
}

回答1:


It all depends, what are you trying to do.

Probably the action stack or action view helpers will do the work for you, but this may be not the best solution, because the dispatch overhead (probably will be removed in ZF2).

The second approach are view helpers with call to the models and actions in the controllers directly. You may use action helpers (and a static call to them) to access controller logic.

Also, see this blog post:

Using Action Helpers To Implement Re-Usable Widgets - phly, boy, phly




回答2:


Since what Kohana's HMVC pattern ultimately does is give you a way to service HTTP requests internally, you can create an adapter Zend_Http_Client that does the same thing. I wrote some proof of concept code to do this once; see zend-http-client-adapter-internal.

An example of calling HelloController from IndexController:

class IndexController extends Zend_Controller_Action
{

    public function indexAction()
    {
        $client = new Zend_Http_Client("http://api.local/hello/?name=Clem");

        $client->setAdapter(new Http_Client_Adapter_Internal(
            $this->getFrontController()
        ));

        $response = $client->request();
        echo $response->getBody();
    }

}

As you can see, instead of Kohana's Request::factory($url), you need to construct the client (the api.local hostname is not used I think, but necessary to satisfy some assertion) and then set its adapter. These two steps could obviously be performed by a wrapper function.



来源:https://stackoverflow.com/questions/3933937/hmvc-in-zend-framework

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!