search
Homephp教程PHP开发Zend Framework action controller usage example

The example in this article describes the usage of Zend Framework action controller. Share it with everyone for your reference, as follows:

Introduction to Action Controller

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

Initialization A more appropriate way to customize instantiation is to use the init() 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(&#39;Pdo_Mysql&#39;,array(
      &#39;host&#39;=>&#39;myhost&#39;,
      &#39;username&#39;=>&#39;user&#39;,
      &#39;password&#39;=>&#39;xxxx&#39;,
      &#39;dbname&#39;=>&#39;website&#39;
    ));
  }
}

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

Accessor

Action controllers can include many contents, such as request objects, response objects, call parameters, and request parameters. These contents can be accessed through the corresponding accessor methods.

The request object can be obtained through the getRequest() method. Executing this method will return a Zend_Controller_Request_Abstract instance.

Code:

$module = $this->getRequest()->getModuleName();//获取模块名称
$controller = $this->getRequest()->getControllerName();//获取控制器名称
$action = $this->getRequest()->getActionName();//获取动作名称

The response object can be obtained through the getResponse() method. Executing this method will return a Zend_Controller_Response_Abstract instance.

The request parameters of the request object 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 method

View initialization

Executing the initView() method will initialize the view object.

Parsing the view

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(&#39;bar&#39;);
    //Renders baz.phtml
    $this->render(&#39;baz&#39;,null,true);//第三个参数,指定是否使用控制器目录作为子目录,true表示不使用
    //Renders my/login.phtml to the &#39;form&#39; segment of the response object
    $this->render(&#39;login&#39;,&#39;form&#39;);
  }
}

Others Method

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

I hope that this article will help everyone use PHP based on the Zend Framework framework Programming helps.

For more Zend Framework action controller usage examples and related articles, please pay attention to the PHP Chinese website!

Statement
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn

Hot AI Tools

Undresser.AI Undress

Undresser.AI Undress

AI-powered app for creating realistic nude photos

AI Clothes Remover

AI Clothes Remover

Online AI tool for removing clothes from photos.

Undress AI Tool

Undress AI Tool

Undress images for free

Clothoff.io

Clothoff.io

AI clothes remover

Video Face Swap

Video Face Swap

Swap faces in any video effortlessly with our completely free AI face swap tool!

Hot Tools

SAP NetWeaver Server Adapter for Eclipse

SAP NetWeaver Server Adapter for Eclipse

Integrate Eclipse with SAP NetWeaver application server.

MinGW - Minimalist GNU for Windows

MinGW - Minimalist GNU for Windows

This project is in the process of being migrated to osdn.net/projects/mingw, you can continue to follow us there. MinGW: A native Windows port of the GNU Compiler Collection (GCC), freely distributable import libraries and header files for building native Windows applications; includes extensions to the MSVC runtime to support C99 functionality. All MinGW software can run on 64-bit Windows platforms.

SecLists

SecLists

SecLists is the ultimate security tester's companion. It is a collection of various types of lists that are frequently used during security assessments, all in one place. SecLists helps make security testing more efficient and productive by conveniently providing all the lists a security tester might need. List types include usernames, passwords, URLs, fuzzing payloads, sensitive data patterns, web shells, and more. The tester can simply pull this repository onto a new test machine and he will have access to every type of list he needs.

Notepad++7.3.1

Notepad++7.3.1

Easy-to-use and free code editor

ZendStudio 13.5.1 Mac

ZendStudio 13.5.1 Mac

Powerful PHP integrated development environment