Custom Zend_Controller routes

Controller pada zend framework 1.x umumnya host/controller/action/, tapi kadang kala kita butuh menampilkan dengan route action yang “serupa” controller atau custom controller. Jadi jika kita punya URL host/company/about maka kita bisa ubahnya dengan host/about yang menampilkan serupa.

Saya asumsikan kita sudah membuat controller company dan action about.

Controller Company

<?php 

class CompanyController extends Zend_Controller_Action 
{  
  public function init(){}  

  public function indexAction()  
  {      
     $compay = "Ini halaman Company....";      
     $this->view->compay = $compay;
  }
 
  public function aboutAction()
  {
     $about = array(
                'Nama pemilik' => 'Jojon',
                'Berdiri Pada tahun' => 1998,
                'Alamat' => 'Jl. Dipati ukur no. xxx',
                'Visi' => 'Blah blah Visi',
                'Misi' => 'Blah blah Misi'
              );
     $this->view->about = $about;

     if ($this->getRequest()->getParam('query')) {
         $query = $this->getRequest()->getParam('query');
         $this->view->query = 'Query: '.$query;

      } else {
         $this->view->query = '';
      }
   }
}

Buat konfigurasinya di Bootstrap


<?php 

class Bootstrap extends Zend_Application_Bootstrap_Bootstrap 
{     
      protected function _initCompanyRoute()     
      {         
          $fr     = Zend_Controller_Front::getInstance();         
          $route  = $fr->getRouter();
          $route->addRoute('aboutByCompany', 
                          new Zend_Controller_Router_Route('/about',  
                      array('controller'=>'company','action'=>'about'))
           );

          // menggunakan parameter
          $route->addRoute('aboutParam', 
                          new Zend_Controller_Router_Route('/about/:query',
                       array('controller'=>'company','action'=>'about')));
       }
}

View index.phtml (dari controller company)

<?php echo $this->company; ?>

View about.phtml

</pre>
<h1>About</h1>
<strong> <?php echo $this->query; ?> </strong>

<hr />

<?php 
 foreach($this->about as $key => $value) :
     echo $key . " : ". $value . "";
 endforeach;
?>

Test Drive:
1. http://host/about
2. http://host/about/helloworld

Referensi:

http://framework.zend.com/manual/1.12/en/zend.controller.router.html