gethinw Posted October 20, 2008 Share Posted October 20, 2008 Hi all, I'm just starting out with Zend and am trying to get my head round a few concepts. Basically, I have a site with a number of different pages (realised as controllers), each of which pulls data from a different database table and displays it. So, the site structure is something like this: public/ /news /biography /links However, a few of these pages do the same thing to the data of their respective tables, and I was wondering how I'd go about reducing the amount of duplicated code. Something along the lines of: class BasicController extends Zend_Controller_Action { //reusable code in here, using a variable $name } --------- class BiographyController extends BasicController { //passes 'biography' to $name variable } ---------- class LinksController extends BasicController { //passes 'links' to $name variable } So, that's the idea, is that a sensible way of going about this? And how would I go about actually passing the variable? Hope that that's clear - let me know if it's not, or if any more information would be useful! Thanks, Gethin Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/ Share on other sites More sharing options...
kazil Posted October 20, 2008 Share Posted October 20, 2008 Never did this, but something like this should help: class BasicController extends Zend_Controller_Action { public static $variable = ''; public function init() { self::$variable = 'assign value'; } } class BioController extends BasicController { public function indexAction() { $bioVar = parrent::$variable; } } Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/#findComment-670306 Share on other sites More sharing options...
gethinw Posted October 21, 2008 Author Share Posted October 21, 2008 Ah yes, that's almost what I'm looking for, but wanting to do it the other way - pass the variable from BioController to BasicController. I've got this at the moment, but it doesn't seem to work: abstract class ATCController extends Zend_Controller_Action { public static $tableName = ''; public function indexAction() { $this->view->headTitle(ucfirst($tableName)); $model = $this->_getModel(); $this->view->content = $model->fetchRow()->Text; } protected function _getModel() { if (null === $this->_model) { // autoload only handles "library" compoennts. Since this is an // application model, we need to require it from its application // path location. require_once APPLICATION_PATH . '/models/ATCTable.php'; $this->_model = new ATCTable(array('name' => $tableName)); } return $this->_model; } } --------------------------- class BiographyController extends ATCController { public function indexAction() { parent::$tableName = 'biography'; parent::indexAction(); } } Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/#findComment-670696 Share on other sites More sharing options...
gethinw Posted October 21, 2008 Author Share Posted October 21, 2008 Sorted, in case anyone is interested: abstract class ATCController extends Zend_Controller_Action { abstract protected function _getName(); //to get table name from child classes public function indexAction() { $this->view->headTitle(ucfirst($this->_getName())); $model = $this->_getModel(); $this->view->content = $model->fetchRow()->Text; } protected function _getModel() { if (null === $this->_model) { // autoload only handles "library" compoennts. Since this is an // application model, we need to require it from its application // path location. require_once APPLICATION_PATH . '/models/ATCTable.php'; $this->_model = new ATCTable(array('name' => strtolower($this->_getName()))); } return $this->_model; } } ------------------ class BiographyController extends ATCController { protected function _getName() { return 'biography'; } } Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/#findComment-670902 Share on other sites More sharing options...
kimpossible Posted August 11, 2009 Share Posted August 11, 2009 if you want to pass a variable to another controller, u can use either of the ff: _redirect() _forward() // works if you want to pass an array class BasicController extends Zend_Controller_Action { public function indexAction() { $name = "alex"; $arrName = array ( 'firsname' => "myFirstName", 'lastname => "myLastName"); // if you want to pass variable name: //STRUCTURE: controller/action/variableName/value $this->_redirect('bio/index/name/$name"); //if you intend to pass an array: //STRUCTURE: 'action', 'controller', $array $this->_forward('index', 'bio', $arrName); } } ----------------------------- class BioController extends Zend_Controller_Action { public function indexAction() { //for sample #1 $name = $this->getRequest()->getParam(''name'); } } hope this could help. Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/#findComment-895333 Share on other sites More sharing options...
albertrosa Posted August 11, 2009 Share Posted August 11, 2009 True you can do just that but it's the duplicate code that we want to limit. Now I tend to create a controller with all the trimings needed especially if it's going to be the normal "C.R.U.D" structure. So Check it I would make a controller with like this. Now this is just a shell for it. but the $_model will be called and initialized as the model. Depending on you Model Structure. I tend to not follow Conventional structures but to keep code simple and easy to read for my projects I abstract and refactor repetative code into functions in the controller. I function 1 code 1 fix. class baseController extends Zend_Controller_Action{ private $_model; public function init(){} public function createAction(){} public function editAction(){} public function deleteAction(){} public function indexAction(){} } if you want to pass a variable to another controller, u can use either of the ff: _redirect() _forward() // works if you want to pass an array class BasicController extends Zend_Controller_Action { public function indexAction() { $name = "alex"; $arrName = array ( 'firsname' => "myFirstName", 'lastname => "myLastName"); // if you want to pass variable name: //STRUCTURE: controller/action/variableName/value $this->_redirect('bio/index/name/$name"); //if you intend to pass an array: //STRUCTURE: 'action', 'controller', $array $this->_forward('index', 'bio', $arrName); } } ----------------------------- class BioController extends Zend_Controller_Action { public function indexAction() { //for sample #1 $name = $this->getRequest()->getParam(''name'); } } hope this could help. Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/#findComment-895765 Share on other sites More sharing options...
Maq Posted August 11, 2009 Share Posted August 11, 2009 You guys do know you resurrected a thread from October 21, 2008. In the future use tags. Quote Link to comment https://forums.phpfreaks.com/topic/129258-zend-controller-reuse/#findComment-895794 Share on other sites More sharing options...
Recommended Posts
Join the conversation
You can post now and register later. If you have an account, sign in now to post with your account.