W moim przypadku applet musi mieć dostęp do bazy danych i jakiegoś szablonu, czyli modelu i widoku. Takie same prawa ma wówczas Controller. Moj kontroler wygląda mniej wiecej tak:
<?php abstract class Vcontroller implements VcontrollerInterface { protected $_oView = NULL; protected $_oConfiguration = NULL; final public function __set($sName, $sValue) { $this->_aData[$sName] = $sValue; } final public function __get($sName) { } final public function setView($sName) { $sViewPatch = Vframe::patchView($sName); $sViewClass = $sName . V_APP_SURFIX_VIEW; if(!Vframe::isUsable($sViewPatch)) throw new VcontrollerException('View "'.$sName.'" does not exists (file "'.$sViewPatch.'" passed)!'); require_once($sViewPatch); if(!class_exists($sViewClass, FALSE)) throw new VcontrollerException('Class of view "'.$sViewClass.'" does not exists in file "'.$sViewPatch.'"!'); $oViewObject = new $sViewClass; return $oViewObject; } final public function setModel($sName) { $sModelPatch = Vframe::patchModel($sName); $sModelClass = $sName . V_APP_SURFIX_MODEL; if(!Vframe::isUsable($sModelPatch)) throw new VcontrollerException('Model "'.$sName.'" does not exists (file "'.$sModelPatch.'" passed)!'); require_once($sModelPatch); if(!class_exists($sModelClass, FALSE)) throw new VcontrollerException('Class of model "'.$sModelClass.'" does not exists in file "'.$sModelPatch.'"!'); $oModelObject = new $sModelClass; return $oModelObject; } final public function getApplet($sName, $sAction = NULL) { $sAppletPatch = Vframe::patchApplet($sName); $sAppletClass = $sName . V_APP_SURFIX_APPLET; $sAppletAction = (($sAction) ? $sAction : V_APP_DEFAULT_ACTION) . V_APP_SURFIX_ACTION; if(!Vframe::isUsable($sAppletPatch)) throw new VcontrollerException('Applet "'.$sName.'" does not exists (file "'.$sAppletPatch.'" passed)!'); require_once($sAppletPatch); if(!class_exists($sAppletClass, FALSE)) throw new VcontrollerException('Class of Applet "'.$sAppletClass.'" does not exists in file "'.$sAppletPatch.'"!'); $oAppletObject = new $sAppletClass; if(!method_exists($oAppletObject, $sAppletAction)) throw new VcontrollerException('Applet method "'.$sAppletAction.'" does not exists in class "'.$sAppletClass.'" ('.$sAppletPatch.')!'); $oAppletObject->$sAppletAction()->display(); return $sAppletContents; } public function getConfig($sConfigName = NULL) { if(!$this->_oConfiguration instanceof VconfigPattern) { $oConfig = new Vconfig(); $sCurrentConfig = ($sConfigName) ? $sConfigName : get_class($this); if(!Vframe::isUsable(Vframe::patchConfig($sCurrentConfig))) return NULL; if(!$oConfig->hasConfig($sCurrentConfig)) $oConfig->setConfig($sCurrentConfig, $sCurrentConfig, FALSE, FALSE); $this->_oConfiguration = $oConfig->getConfig($sCurrentConfig); } return $this->_oConfiguration; } } ?>
Gdyby się głebiej zastanowić, mój applet zbudowany byłby z kontolera i akcji. Fajnie jakby nie wyświetlał danych z szablonu, ale je zwracał. Postanowiłem więc zrobić takie coś:
<?php abstract class Vapplet extends Vcontroller { public function display() { return; } } ?>
Applet dziedziczy z kontrollera ale nie wyswietla danych (specjalnie mam funkcję display() która jest w modelu, bo tak jest wywoływany szablon akcji w dispatcherze.
zwróćmy jeszcze raz uwagę na odpalenie appletu w akcji:
<?php final public function getApplet($sName, $sAction = NULL) { $sAppletPatch = Vframe::patchApplet($sName); $sAppletClass = $sName . V_APP_SURFIX_APPLET; $sAppletAction = (($sAction) ? $sAction : V_APP_DEFAULT_ACTION) . V_APP_SURFIX_ACTION; if(!Vframe::isUsable($sAppletPatch)) throw new VcontrollerException('Applet "'.$sName.'" does not exists (file "'.$sAppletPatch.'" passed)!'); require_once($sAppletPatch); if(!class_exists($sAppletClass, FALSE)) throw new VcontrollerException('Class of Applet "'.$sAppletClass.'" does not exists in file "'.$sAppletPatch.'"!'); $oAppletObject = new $sAppletClass; if(!method_exists($oAppletObject, $sAppletAction)) throw new VcontrollerException('Applet method "'.$sAppletAction.'" does not exists in class "'.$sAppletClass.'" ('.$sAppletPatch.')!'); $oAppletObject->$sAppletAction()->display(); return $sAppletContents; } ?>
przypisanie tego jest równie proste:
<?php class index_Vcontroller extends Vcontroller { public function index_Action() { $oView = $this->setView('example'); $oView->menu = $this->getApplet('menu_example'); return $oView; } } ?>
U mnie poki co wygląda to w zaprezentowany sposob.
Jak wygląda to w Waszych frameworkach?