加载中...

7.7 添加控制器 AlbumController


添加文件:/module/Album/src/Controller/AlbumController.php 具体内容如下:

namespace Album\Controller;
use Zend\Mvc\Controller\AbstractActionController;
use Zend\View\Model\ViewModel;
use Album\Model\Album;
use Album\Form\AlbumForm;

class AlbumController extends AbstractActionController{
    protected $albumTalbe;
    public function indexAction(){
        $paginator = $this->getAlbumTalbe()->fetchAll(true);
        $paginator->setCurrentPageNumber((int)$this->params()->fromQuery('page',1));
        $paginator->setItemCountPerPage(5);
        return new ViewModel(array('paginator'=>$paginator));
    }

    public function addAction(){
        $form = new AlbumForm();
        $form->get('submit')->setValue('Add');
        $request = $this->getRequest();
        if($request->isPost()){
            $album = new Album();
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());
            if($form->isValid()){
                $album->exchangeArray($form->getData());
                $this->getAlbumTalbe()->saveAlbum($album);
                return $this->redirect()->toRoute('album');
            }
        }
        return array('form'=>$form);
    }

    public function editAction(){
        $id = (Int) $this->params()->fromRoute('id',0);
        if(!$id){
            return $this->redirect()->toRoute('album',array('action'=>'add'));
        }
        try{
            $album = $this->getAlbumTalbe()->getAlbum($id);
        }catch(\Exception $e){
            return $this->redirect()->toRoute('album',array('action'=>'index'));
        }
        $form = new AlbumForm();
        $form->bind($album);
        $form->get('submit')->setAttribute('value', 'Edit');
        $request = $this->getRequest();
        if($request->isPost()){
            $form->setInputFilter($album->getInputFilter());
            $form->setData($request->getPost());
            if($form->isValid()){
                $this->getAlbumTalbe()->saveAlbum($form->getData());
                return $this->redirect()->toRoute('album');
            }
        }
        return array('id'=>$id,'form'=>$form);
    }

    public function deleteAction(){
        $id = (Int) $this->params()->fromRoute('id',0);
        if(!$id){
            return $this->redirect()->toRoute('album');
        }
        $request = $this->getRequest();
        if($request->isPost()){
            $del = $request->getPost('del','No');
            if($del=='Yes'){
                $id = (Int)$request->getPost('id');
                $this->getAlbumTalbe()->deleteAlbum($id);
            }
            return $this->redirect()->toRoute('album');
        }
        return array('id'=>$id,'album'=>$this->getAlbumTalbe()->getAlbum($id));

    }
    public function getAlbumTalbe(){
        if(!$this->albumTalbe){
            $sm = $this->getServiceLocator();
            $this->albumTalbe = $sm->get('Album\Model\AlbumTable');
        }
        return $this->albumTalbe;
    }

}

代码解释:

public function indexAction(){} album默认访问action,也是album列表action

public function addAction(){} 添加album 的 action

public function editAction(){} 编辑修改album的action

public function deleteAction(){} 删除album 的action

lpublic function getAlbumTalbe(){} 设置数据库网关


还没有评论.