ZF Framework Controllers Custom Action_PHP Tutorial
The front-end controller is the hard work in MVC construction, because it instantiates objects, triggers events, establishes default behaviors, etc. Its main purpose is to handle all requests entering the application. The design pattern of the front-end controller is applied in different MVC frameworks. The front-end controller (Front Controller) we refer to in Zend Framework actually refers to the Zend_Controller_Front class, because this class implements the front-end controller pattern; please note that What's interesting is that the front-end controller design is a singleton mode (Singleton), which means that it implements the singleton design mode, that is, there can only be one instantiated front-end controller, that is, we cannot directly instantiate the Front Controller. , but take one.
Below we implement a simple controller jump and distribution.
IndexController.php is created in the controllers folder, and the index.phtml file is created in the view folder. Enter http://localhost/NowaMagicFrame1.0/ in the address bar to browse.
<?php require('CommonController.php'); class IndexController extends Zend_Controller_Action { function init() { //parent::init(); $this->registry = Zend_Registry::getInstance(); $this->view = $this->registry['view']; $this->view->baseUrl = $this->_request->getBaseUrl(); } public function indexAction() { //这里给变量赋值,在index.phtml模板里显示 $this->view->bodyTitle = 'NowaMagic Frame 1.0'; echo $this->view->render('index.phtml');//显示模版 } /** * 新闻 * */ public function newsAction(){ //这里给变量赋值,在news.phtml模板里显示 $this->view->bodyTitle = 'NowaMagic Frame 新闻'; echo $this->view->render('news.phtml');//显示模版 } } ?>
Now if I want to access the news page, I can access it through IndexContriller, because it has the newsAction() method to achieve forwarding. The specific access method is http://localhost/NowaMagicFrame1.0/index/news/
But this URL does not look as good as imagined. The ideal URL should look like this: http://localhost/NowaMagicFrame1.0/news/
How to achieve it? We need to create a NewsController.php
<?php class NewsController extends Zend_Controller_Action { function init() { $this->registry = Zend_Registry::getInstance(); $this->view = $this->registry['view']; $this->view->baseUrl = $this->_request->getBaseUrl(); } /** * 标签首页 * */ function indexAction(){ echo $this->view->render('news.phtml'); } } ?>
Just add an indexAction to this file.

Hot AI Tools

Undresser.AI Undress
AI-powered app for creating realistic nude photos

AI Clothes Remover
Online AI tool for removing clothes from photos.

Undress AI Tool
Undress images for free

Clothoff.io
AI clothes remover

AI Hentai Generator
Generate AI Hentai for free.

Hot Article

Hot Tools

Notepad++7.3.1
Easy-to-use and free code editor

SublimeText3 Chinese version
Chinese version, very easy to use

Zend Studio 13.0.1
Powerful PHP integrated development environment

Dreamweaver CS6
Visual web development tools

SublimeText3 Mac version
God-level code editing software (SublimeText3)

Hot Topics

Using ZendFramework with PHP: Quick Start Guide ZendFramework is an open source, PHP-based web application framework that is powerful and easily extensible. ZendFramework contains many useful components that can help you build efficient web applications. This article will introduce how to use ZendFramework in PHP to help you get started quickly. Install ZendFramewo

Implementing efficient database queries through ZendFramework middleware Introduction In the development process, database queries are an inevitable part. An efficient database query can greatly improve system performance and user experience. ZendFramework is a widely used PHP framework with powerful database operation functions. This article will introduce how to implement efficient database queries through ZendFramework middleware and provide corresponding code examples. 1. Understanding ZendF

ZendFramework Middleware: Adding OAuth and OpenID Login Support to Applications User authentication is a critical feature in today's Internet applications. In order to provide better user experience and security, many applications choose to integrate third-party login services, such as OAuth and OpenID. In ZendFramework, we can easily add OAuth and OpenID login support to our applications through middleware. First, we need to install Ze

ZendFramework is an open source framework based on PHP that provides many powerful tools and components for building scalable web applications. This article will introduce how to use ZendFramework's middleware to add social login functionality to web applications. Middleware is code that is executed before or after a request enters your application. It allows developers to customize and extend the process of handling requests. ZendFramework provides a flexible way to

ZendFramework2 is a popular PHP programming framework that provides a wealth of functions and modules, allowing PHP developers to build high-quality Web applications more conveniently. This article will introduce some common ZendFramework2 operations to help you better use this framework. MVC pattern In ZendFramework2, the Model-View-Controller (MVC) pattern is the most common architecture. The MVC pattern is a

ZendFramework middleware: Adding Alipay and WeChat payment functions to applications Introduction: With the popularity of mobile payments, Alipay and WeChat payment have become essential payment methods in many applications. This article will introduce how to use ZendFramework middleware to add Alipay and WeChat payment functions to the application. By studying this article, you will learn how to use middleware to simplify the payment process and apply it to your actual projects. 1. Preparation Before starting, you

When you decide to develop an ERP system, choosing a suitable framework is crucial. Here we will compare the two PHP frameworks CodeIgniter and ZendFramework to help you find a framework that is more suitable for your ERP system development. CodeIgniter and ZendFramework are popular PHP frameworks. They all provide many features and are extensible and maintainable. However, these two frameworks differ significantly in some aspects and are more suitable for some applications.

PHP is a widely used programming language, and ZendFramework2 is a popular PHP framework. This framework provides PHP programmers with powerful tools to build high-quality, maintainable and scalable applications. This article will introduce how to use ZendFramework2 in PHP programming. What is ZendFramework2? ZendFramework2 is a popular PHP framework for building web applications and services.
