SoFunction
Updated on 2025-03-09

Example of usage of Zend Framework action controller

This article describes the usage of Zend Framework action controller. Share it for your reference, as follows:

Introduction to the action controller

In order to use the Zend_Controller_Action class, it needs to be subclassed in the actual controller class.

Code:

<?php
class FooController extends Zend_Controller_Action{
  public function barAction(){
    //do something
  }
  public function bazAction(){
    //do something
  }
}

Note: The above FooController class defines two actions, bar and baz.

Object Initialization

A more appropriate way to initialize a custom instantiation is to useinit() method. This method is the last call task in __construct().

Code:

<?php
class FooController extends Zend_Controller_Action{
  public function init(){
    $this->db = Zend_Db::factory('Pdo_Mysql',array(
      'host'=>'myhost',
      'username'=>'user',
      'password'=>'xxxx',
      'dbname'=>'website'
    ));
  }
}

Note: The above code implements connection to the database while initializing the object.

Accessories

The action controller can include many contents, such as request object, response object, call parameters, and request parameters. These contents can be accessed through the corresponding accessor method.

The request object can be passedgetRequest()Method to obtain, executing the method will return a Zend_Controller_Request_Abstract instance.

Code:

$module = $this-&gt;getRequest()-&gt;getModuleName();//Get the module name$controller = $this-&gt;getRequest()-&gt;getControllerName();//Get the controller name$action = $this-&gt;getRequest()-&gt;getActionName();//Get the action name

The response object can be passedgetResponse()Method to obtain, executing the method will return a Zend_Controller_Response_Abstract instance.

The request parameters of the request object include any GET or GET or _POST parameters. To read these parameters, you can use_getParam($key) or _getAllParams()method.

View integration method

View Initialization

implementinitView()The method will initialize the view object.

Analyze view

render()Methods are used to parse views

Code:

&lt;?php
class MyController extends Zend_Controller_Action{
  public function fooAction(){
    //Renders my/
    $this-&gt;render();
    //Renders my/
    $this-&gt;render('bar');
    //Renders 
    $this-&gt;render('baz',null,true);//The third parameter specifies whether to use the controller directory as a subdirectory. True means not to use it    //Renders my/ to the 'form' segment of the response object
    $this-&gt;render('login','form');
  }
}

Other methods

_forword(), this method performs another action
_redirect(), the method redirects to another place

For more information about Zend, please visit the special topic of this site:Zend FrameWork Framework Introduction Tutorial》、《Summary of excellent development framework for php》、《Yii framework introduction and common techniques summary》、《ThinkPHP Introduction Tutorial》、《PHP object-oriented programming tutorial》、《PHP+mysql database operation tutorial"and"Summary of common database operation techniques for php

I hope that this article will be helpful to everyone's PHP programming based on the Zend Framework framework.