什么是前端控制器,如何在PHP中实现? [英] What is a Front Controller and how is it implemented in PHP?

查看:100
本文介绍了什么是前端控制器,如何在PHP中实现?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

首先,我是PHP的初学者.并在这里发布了一个问题: 重构项目中的require_once文件 .我已经尝试了尽可能多地了解Front控制器,但无法了解它的工作原理甚至是全部.

First of all, i'm a beginner to PHP. And have posted a question here : Refactoring require_once file in a project . I've tried to read about Front controller as much as i can, but can't get how it works or even what's all about.

有人可以简要解释一下它是如何工作的,以及全部内容吗?

Can somebody explain in brief how it works and what's all about?

谢谢.

推荐答案

Front Controller指的是一种设计模式,其中应用程序中的单个组件负责处理对应用程序其他部分的所有请求.它集中了应用程序其余部分所需的通用功能.模板,路由和安全性是Front Controller功能的常见示例.使用这种设计模式的好处是,当需要更改这些功能的行为时,只需修改应用程序的一小部分即可.

Front Controller refers to a design pattern where a single component in your application is responsible for handling all requests to other parts of an application. It centralizes common functionality needed by the rest of your application. Templating, routing, and security are common examples of Front Controller functionality. The benefit to using this design pattern is that when the behavior of these functions need to change, only a small part of the application needs to be modified.

以网络术语来说,对某个域的所有请求都由一个入口点(前端控制器)处理.

In web terms, all requests for a domain are handled by a single point of entry (the front controller).

一个极其简单示例,仅一个前控制器的路由功能.使用Apache提供的PHP看起来像这样.最重要的步骤是将所有请求重定向到前端控制器:

An extremely simple example of only the routing functionality of a front-controller. Using PHP served by Apache would look something like this. Most important step is to redirect all requests to the front controller:

.htaccess

RewriteEngine On
RewriteRule . /front-controller.php [L]

front-controller.php

<?php

switch ($_SERVER['REQUEST_URI']) {
    case '/help':
        include 'help.php';
        break;
    case '/calendar':
        include 'calendar.php';
        break;
    default:
        include 'notfound.php';
        break;
}

这篇关于什么是前端控制器,如何在PHP中实现?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆