About the use of the Zend Framework action Controller

Source: Internet
Author: User
This article mainly introduced the Zend Framework action controller usage, combined with the case form analysis of the function of the action controller, the use of procedures, related skills and considerations, the need for friends can refer to the following

The examples in this paper describe the Zend Framework action controller usage. Share to everyone for your reference, as follows:

Introduction to Motion Controllers

In order to use the Zend_controller_action class, it is necessary to subclass it in the actual controller class.

Code:

<?phpclass Foocontroller extends zend_controller_action{public  function baraction () {    //do something  } public  function Bazaction () {    //do something  }}

Description: The Foocontroller class above defines two actions, bar and Baz.

Object initialization

Initializing a more appropriate method to customize the instantiation is using the init () method . The method is the last invocation of the task in __construct ().

Code:

<?phpclass Foocontroller extends zend_controller_action{public  function init () {    $this->db = zend_db:: Factory (' Pdo_mysql ', Array (      ' host ' = ' myhost ',      ' username ' = ' user ',      ' password ' = ' xxxx '      , ' dbname ' = ' website '    );}  }

Description: The code above initializes the object and implements a connection to the database.

Accessors

An action controller can include a number of content, such as a request object, a response object, a call parameter, a request parameter. This content can be accessed through the appropriate accessor methods.

The request object can be obtained through the Getrequest () method, and executing the method returns an Zend_controller_request_abstract instance.

Code:

$module = $this->getrequest ()->getmodulename ();//Get module name $controller = $this->getrequest () Getcontrollername ();//Get controller name $action = $this->getrequest ()->getactionname ();//Get Action name

The response object can be obtained through the GetResponse () method, and executing the method returns an Zend_controller_response_abstract instance.

Request parameters for request objects include any get or get or _post parameters. In order to read these parameters, you can use the _getparam ($key) or _getallparams () method.

View Integration Methods

View Initialization

Execution of the Initview () method initializes the View object.

Parse view

The render () method is used to parse the view

Code:

<?phpclass Mycontroller extends zend_controller_action{public  function fooaction () {    //renders my/ foo.phtml    $this->render ();    Renders my/bar.phtml    $this->render (' Bar ');    Renders baz.phtml    $this->render (' Baz ', null,true);//The third parameter, specifying whether to use the Controller directory as a subdirectory, true to not use    //renders my/ Login.phtml to the ' form ' segment of the response object    $this->render (' Login ', ' form ');}  }

Other methods

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

The above is the whole content of this article, I hope that everyone's learning has helped, more relevant content please pay attention to topic.alibabacloud.com!

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.