Jump to content

Archived

This topic is now archived and is closed to further replies.

gethinw

Zend controller reuse

Recommended Posts

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

Share this post


Link to post
Share on other sites

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;
    }
}

 

Share this post


Link to post
Share on other sites

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();
  }
}

Share this post


Link to post
Share on other sites

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';
  }
}

Share this post


Link to post
Share on other sites

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.  :D

Share this post


Link to post
Share on other sites

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.  :D

Share this post


Link to post
Share on other sites

You guys do know you resurrected a thread from October 21, 2008.

 

In the future use


tags.

Share this post


Link to post
Share on other sites

×
×
  • Create New...

Important Information

We have placed cookies on your device to help make this website better. You can adjust your cookie settings, otherwise we'll assume you're okay to continue.