开发者

Zend Framework: Controllers in separate directories

I'm quite new in Zend framework, but quickly learning. I've encountered the following problem, but I don't really know if my solution is good :)

I've created an application which uses widgets. Widget is a class which implements Widget_Interface and is executed by Widget_Manager.

Widgets can be loaded via WidgetController (which calls Widget_Manager, etc). Now the problem I encountered is: widgets can also be configured, and to make the code more transparent, I'd like a widget to have its own controller (currently, it is only a class). But the problem is, I'd like all widget configurations to be addressed via WidgetController, and then passed to specific widget controller.

An example: let's say I've got a widget named 'scrobbler'. Now w开发者_开发知识库hen configuring it in the UI, I'd like to make Ajax request with updated settings. I could make a request like http://myapp.com/scrobbler/update-info/, so the framework would run ScrobblerController and I'd process the information from here on. My idea is to make a request on http://myapp.com/widget/update/scrobbler/, so the framework runs WidgetController. WidgetController would then call ScrobblerController and pass other parameters.

I'm aware of _forward() function in Zend_Controller, but I'd like to have widget controllers and my application's controllers separated (let's say application controllers in /application/controllers and widget controllers in /application/controllers/widgets).

Is it possible to make this and what do I have to add to the Zend framework configuration? Hope I didn't complicate too much :)

Nice day

Edit: Solved this using modular structure, and moved common classes into root directory.


You coud probably utilize Controller helpers instead of controllers in this case. So let's say that WidgetController is responsible for updating all types of widgets. The updateAction would need to find information on which widget type you wish to configure, this is the scrobbler parameter. You would need to name this parameter so it can be accessed easily. This can be done by either adding a route or adding the name before scrobbler in the uri.

Solution 1: Add a route:

In Bootstrap:

public function __initRoutes () {
    $route = new Zend_Controller_Router_Route(
        'widget/update/:type',
        array (
            'controller' => 'widget',
            'action' => 'update'
        ),
        array (
            'type' => '[a-z_-]*'
        )
    );
    /* @var $fc Zend_Controller_Front */
    $fc = $this->bootstrap('FrontController')->getResource('FrontController');
    /* @var $router Zend_Controller_Router_Rewrite */
    $router = $fc->getRouter();
    $router->addRoute('update-widget', $route);
}

Solution 2: Add the parameter name in the uri:

Make requests to /widget/update/type/widgetName instead.


Now, in the WidgetController::updateAction, you can fetch the widget to update using $this->_getParam('type').

So the code could look something like:

class WidgetController extends Zend_Controller_Action
{
  public function updateAction ()
  {
     $widgetName = $this->_getParam('type');
     $this->view->result = $this->_helper->Widgets->update($widgetName);
  }
}

class App_Controller_Helper_Widgets extends Zend_Controller_Action_Helper
{
  public function update($widgetName)
  {
     $widgetManager = new App_Model_WidgetManager();
     $widget = $widgetManager->load($widgetName);
     $widget->setOptions($this->getRequest()->getParams());
     return $widget->save();
  } 
}
0

上一篇:

下一篇:

精彩评论

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

最新问答

问答排行榜