Refactoring the Bug Controller listAction to Load the Paginator

Now you need to refactor the BugController's listAction() one more time to fetch the paginator and pass it to the view. Note that as you develop more with the framework, you will see a pattern emerge where you cycle from the model to the controller to the view, working on all three components simultaneously to build and refactor functionality.

You first need to refactor how the controller is handling the request parameters in listAction(). This is because the sort and filter criteria are going to be appended to the page navigation links, so the controller needs to fetch these regardless of whether the request method is a POST.

Now you need to replace the fetchBugs() method call with fetchPaginatorAdapter(). Once you have this adapter, create a new instance of Zend_Paginator, passing the adapter to its constructor. Then you set the page size, which will be statically set to ten rows, and the page number, which you will set by passing the parameter page. Once the paginator is configured, pass it to the view to render, as shown in Listing 4-22.

Listing4-22. The UpdatedlistAction() Method in application/controllers/BugController.php public function listAction() {

// get the filter form

$listToolsForm = new Form_BugReportListToolsForm(); $listToolsForm->setAction('/bug/list'); $listToolsForm->setMethod('post'); $this->view->listToolsForm = $listToolsForm;

// set the sort and filter criteria. you need to update this to use the request,

// as these values can come in from the form post or a url parameter

$sort = $this->_request->getParam('sort', null);

$filterField = $this->_request->getParam('filter_field', null);

$filterValue = $this->_request->getParam('filter');

$filter[$filterField] = $filterValue; }else{

// now you need to manually set these controls values $listToolsForm->getElement('sort')->setValue($sort); $listToolsForm->getElement('filter_field')->setValue($filterField); $listToolsForm->getElement('filter')->setValue($filterValue);

// fetch the bug paginator adapter $bugModels = new Model_Bug();

$adapter = $bugModels->fetchPaginatorAdapter($filter, $sort);

$paginator = new Zend_Paginator($adapter);

// show 10 bugs per page

$paginator->setItemCountPerPage(l0);

// get the page number that is passed in the request.

//if none is set then default to page 1.

$page = $this->_request->getParam('page', l);

$paginator->setCurrentPageNumber($page);

// pass the paginator to the view to render

$this->view->paginator = $paginator;

0 0

Post a comment

  • Receive news updates via email from this site