Zend Framework Action Controller Usage Example _php instance

Source: Internet
Author: User
Tags zend zend framework

This example describes the Zend Framework action controller usage. Share to everyone for your reference, specific as follows:

Introduction to Action Controllers

In order to use the Zend_controller_action class, you need to subclass it in the actual controller class.

Code:

<?php
class Foocontroller extends zend_controller_action{public
  function baraction () {
    //do something
  }
  Public Function bazaction () {
    //do something
  }
}

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

Object initialization

Initializing a more appropriate way to customize the instantiation is using the init () method . The method is the last invocation 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 ')
  }


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

Access device

An action controller can include many things, such as request objects, response objects, call parameters, request parameters. The content can be accessed through the appropriate accessor methods.

The request object can be obtained through the Getrequest () method, which will return a 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, which will return a zend_controller_response_abstract instance.

The request parameters for the request object include any get or get or _post parameters. To read these parameters, you can use either the _getparam ($key) or the _getallparams () method.

View Integration Methods

View Initialization

The execution of the Initview () method initializes the View object.

Parsing views

The render () method is used to parse the view

Code:

<?php
class 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 specifies whether the controller directory is used as a subdirectory, and True indicates that the//renders my/is not used
    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

More interested in Zend related content readers can view the site topics: "The introduction of the Zend Framework frame", "PHP Excellent Development Framework Summary", "Yii framework Introduction and common skills Summary", "thinkphp Introductory Course", "PHP object-oriented Programming Program , "Php+mysql Database operation Introduction Tutorial" and "PHP common database Operation Skills Summary"

I hope this article will help you with the PHP program design based on the Zend Framework.

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.