0

使用 Zend 框架,如果我想从我的应用程序和通过 REST 接收相同的数据,这将由相同的操作控制器处理,即

articlesController extends Zend_Controller_Action
{
    listAction()
    {
        // Service layer get data

        // REST request return results in JSON

        // Normal request return view
    }
}

还是应该在单独的控制器中?

articlesController extends Zend_Controller_Action
{
    listAction()
    {
        // Service layer get data
        // Returns view
    }
}

articlesController extends Zend_Rest_Controller
{
    getAction()
    {
        // Service layer get data
        // Returns view
    }
}

希望这是有道理的

多谢你们

4

1 回答 1

0

In a default MVC Zend Framework setup the Controller and Action names are linked to the path. The router examines the path and then dispatches to the appropriate controller and action.

With that in mind it doesn't really matter how you set this up. It only depends how you like to structure your paths.

If you have certain information in your path with the additional parameters you could combine everything within one action. Your first param could be "api" for REST request or if your additional params are in the URI path you have a regular request but if the params are in a GET array you have a API REST request. That would all work for your first example and I guess you already thinking that way.

For me it would be more appropriate to have an API path, though. With that you will have a API controller and the corresponding actions. In your second example this would mean your API controller looks more like this

apiController extends Zend_Rest_Controller {
    articlesAction() {
         // your REST data here
    }
}

// URI path: /api/articles

Note that you cannot have two controllers with the same name.

于 2012-07-13T14:29:04.510 回答