Featured post
php - Zend Framework: Controllers in separate directories -
i'm quite new in zend framework, learning. i've encountered following problem, don't know if solution :)
i've created application uses widgets. widget class implements widget_interface , executed widget_manager.
widgets can loaded via widgetcontroller
(which calls widget_manager, etc). problem encountered is: widgets can configured, , make code more transparent, i'd widget have own controller (currently, class). problem is, i'd widget configurations addressed via widgetcontroller
, , passed specific widget controller.
an example: let's i've got widget named 'scrobbler'. when configuring in ui, i'd make ajax request updated settings. make request http://myapp.com/scrobbler/update-info/
, framework run scrobblercontroller
, i'd process information here on. idea make request on http://myapp.com/widget/update/scrobbler/
, framework runs widgetcontroller
. widgetcontroller
call scrobblercontroller
, pass other parameters.
i'm aware of _forward()
function in zend_controller
, i'd have widget controllers , application's controllers separated (let's application controllers in /application/controllers , widget controllers in /application/controllers/widgets).
is possible make , have add zend framework configuration? hope didn't complicate :)
nice day
edit: solved using modular structure, , moved common classes root directory.
you coud utilize controller helpers instead of controllers in case. let's widgetcontroller responsible updating types of widgets. updateaction
need find information on widget type wish configure, scrobbler
parameter. need name parameter can accessed easily. can done either adding route
or adding name before scrobbler
in uri.
solution 1: add 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 parameter name in uri:
make requests /widget/update/type/widgetname
instead.
now, in widgetcontroller::updateaction, can fetch widget update using $this->_getparam('type')
.
so code 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(); } }
- Get link
- X
- Other Apps
Comments
Post a Comment