问题标签 [front-controller]

For questions regarding programming in ECMAScript (JavaScript/JS) and its various dialects/implementations (excluding ActionScript). Note JavaScript is NOT the same as Java! Please include all relevant tags on your question; e.g., [node.js], [jquery], [json], [reactjs], [angular], [ember.js], [vue.js], [typescript], [svelte], etc.

0 投票
2 回答
445 浏览

php - 避免在前端控制器中使用大的 switch 语句

我有一个更大的应用程序,它在 php 中有一个 Frontcontroller 来处理传入的 ajax 请求。我正在考虑一种处理 Action->Method 映射的好方法,这个控制器负责实例化其他类并在那里执行方法。

开关太大了,太丑了。我正在考虑创建一个数组并简单地做:

但不确定这会有多高效,或者是否有任何其他更好的选择,性能很重要,因为该控制器处理大量传入请求。

谢谢!

0 投票
2 回答
2659 浏览

c# - 使用 IHttpAsyncHandler 高效转发 HTTP 请求

我正在开发一个基于 Martin Fowler 模式的 HTTP 前端控制器(链接)。在我的情况下,控制器具有以下职责: - 解组封装数据 - 授权请求 - 记录 - 将请求中继/转发到另一台服务器

想到了以下可能的解决方案:-(同步)IHttpHandler,使用 WebClient 或 HttpWebRequest 类转发请求 -(异步)IHttpListener(非 IIS 解决方案)-(异步)IHttpAsyncHandler

理想的情况是 FC 可以处理大量并发请求 (>10000 TPS) 而不会烧毁 CPU。

为了测试解决方案,我创建了一个小型框架,该框架有 3 个发出请求的客户端、一个位于中间的前端控制器和 2 个响应 FC 传递的请求的服务器。该框架对 3 个场景进行了基准测试,首先它测试了具有小负载的快速响应,其次:具有大负载 (> 100KB) 的快速响应,最后是它的慢响应 (>3 秒) 和小负载的测试。

在使用同步 HTTP 处理程序的最后一次测试中,每秒事务数 (TPS) 降至最低值 (<25 TPS)。我的猜测是这是由于处理程序在等待响应时阻塞了线程。为了克服这个问题,我开始实现一个异步处理程序(参见下面的代码)。问题是,这简单是行不通的。

想到的最后一个(未经测试的)解决方案是使用 HttpListener 类。猜测这是更轻量级的解决方案,具有对并发性的细粒度控制。我看过 José F. Romaniello (链接) 使用 RX 框架的示例实现。

我的问题是,为什么处理程序代码不起作用?这是最有效的方法吗?还是我应该支持 HttpListener 解决方案。

异步 HTTP 处理程序代码:

0 投票
1 回答
257 浏览

symfony1 - 所有应用程序请求都由 symfony 1.4 中的同一个前端控制器分派

我只是面临一些奇怪的情况。我在 symfony 1.4 中有一个项目,它有两个应用程序(api & admin)。我曾经在两个操作系统中工作,在我的办公室里,我在家里有 ubuntu 和 window 7。

在办公室(ubuntu 11.10)一切正常,而在家里(带有 XAMPP 的 Window 7),当我从 API 应用程序(比如说http://www.domain.local/api_dev.php/users.xml)请求一个 url 时,给出答案的前端控制器是管理员(admin_dev.php)。结果我得到 404 错误,因为该路由不是在管理应用程序中定义的,而是在 API 中定义的。

我已经丢弃了项目副本中的错误,或者因为我使用 git 进行版本控制。该 api 应用程序由sfDoctrineRestGeneratorPlugin提供支持,并且以前可以正常工作,但在引入错误时我没有注意到,因为在办公室一切正常。我曾尝试清除缓存,再次从办公室克隆该项目,但没有成功。

在我看来这不是 XAMPP 问题,因为它之前工作正常并要求http://www.domain.local/api_dev.php/product.xml给我这些日志条目:

admin_dev.log

Apr 03 14:42:09 symfony [err] {sfError404Exception} Empty module and/or action after parsing the URL "/product.xml" (/).

api_dev.log [空日志文件]

如果我请求: http://www.domain.local/api_dev.php/它会将我发送到管理主页,但如果我请求:( http://www.domain.local/api_dev.php注意缺少斜线)它会转到 API 中的默认索引路由。在 api_dev.log 我得到了预期的条目。这让我想到了一个路线问题,但没有提出任何想法。

在这两个应用程序中,我都有这样的默认主页路由:

-API-

-行政-

感谢您的聆听和时间任何帮助或建议将不胜感激。

谢谢大卫

0 投票
0 回答
139 浏览

zend-framework - ZF - 动态更改默认控制器

如果请求来自移动设备,我需要加载不同的控制器作为默认控制器。我试图在插件中获取前端控制器实例并根据设备检测在那里设置默认值,但它不起作用。

有没有办法动态更改 FC 设置?

编辑:

插件已注册并且可以工作,但其余代码没有

0 投票
1 回答
3183 浏览

php - 如何使用 .htaccess 工作 php 前端控制器?

我使用查询字符串做了前端控制器。

前任:

但我需要在不使用查询字符串的情况下这样做。

0 投票
1 回答
1720 浏览

spring - 带有前端控制器的 Spring MVC

我正在使用带有 multiActionController 的 MVC 开发 Web 服务器。但是,现在我想用前端控制器处理任何请求( /* )。之后,这个 Front Controller 会将这个请求转发给 ModelAndView 中的 View。但是,用谷歌搜索了很多次后,我仍然不知道如何实现它。我感谢您的帮助。

0 投票
0 回答
1356 浏览

php - PHP front controller in nginx

I have a wiki that hosts user-generated content with URLs like /wiki/view/pagename and /wiki/modify/pagename. I'm using an nginx configuration that goes something like:

It's been working great and as far as i can tell, this is the recommended approach. However, today, a user created a page named "whatever.php", so it needs the URLs /wiki/view/whatever.php to be redirected to my /wiki/index.php... but it gets caught in the second location block and returns a 404 to the user-agent.

Does anyone have any suggestions? Can i add an extra location block to rewrite *.php to the main script somewhere in such a way that won't affect actually routing pages? I still want to use nginx to serve static content inside the /wiki/ directory and to preserve the behaviour of everything outside this directory.

Repost of this dead forum thread

0 投票
2 回答
1012 浏览

php - Multi-level controllers for MVC architecture

I just encountered one of the limitations of the MVC architecture I'm currently using for my applications. Currently, my URLs look like this:

Each request arrives at the front controller, which loads the requested controller class from the URL, and executes the action (method) of it. This works fine, until you need to start using nested controllers.

Example: There's a 'users' controller which holds methods like createUser(), editUser(), deleteUser() etc. All perfectly possible with the current URL structure... But what if we also need to manage user types? We would need a separate controller 'usertypes' which also holds methods like createUserType(), editUserType()... However, since user types are a part of users, the 'usertypes' controller should be nested inside the users controller, as this:

With the current URL structure however, this is not possible... How can I make use of nested (or multi-level if you will) controllers?

UPDATE: here's a basic representation of the application I'm working on: It's a basic business application for the administration department where they can add, view, edit and delete data from 3 categories (Promotions, Mailings and Cardholders). Each category represents a table in the database and has its own distinct fields. Accounts need to be created by the admin, users cannot create an account themselves and they can't consult their user profile.

For each of those categories I've made a controller, which holds actions like add(), edit(), getAll(), getSingle(), delete()... Each of those actions calls the appropriate methods from the Model and renders the corresponding View(s).

This was all possible with the current URL structure, which had URL's like:

Recently they asked me to make it possible to also manage types of promotions, mailings and cardholders. Right now they already have a School Discount, a 20% Discount etc... But they want to add more as they wish.

This means i need a PromotionTypes controller, which also holds actions like add(), getAll(), getSingle(), delete()... It would be nice if the PromotionTypes controller could be nested in the original promotions controller, which enables URL's as so:

instead of

With my current front loader, this is not possible, since the first part of the URL automatically gets treated as the controller, and the second part as the action to execute from it.

0 投票
1 回答
427 浏览

php - 将 CSS 和 JS 请求路由到前端控制器(性能测量)

我已经构建了一个简单的 MVC 库。有一个前端控制器。htaccess 文件将所有请求发送到前端控制器,除了图像、js 和 css。但是我有一个问题,我想用 PHP 构建 JS 和 CSS 文件。(虽然有时不是每次。)

为此,我计划向前端控制器发送 CSS 和 JS 文件请求,然后另一个 PHP 将“包含”相应的 CSS 或 JS 文件,并将其返回给客户端。当包含 CSS 或 JS 时,它也会被 PHP 解释器处理。

虽然让我想到的是性能。我现在只是在做开发,我无法看到当成千上万的请求到来时系统会如何响应这种技术。因为当你构建一个大系统时,很难立即改变。所以我想在早期阶段将这些功能构建到 MVC 中。

如果有人尝试过这样的技术,我想听听性能测量。

0 投票
2 回答
603 浏览

php - 使用前端控制器模式和 MVC 嵌套控制器

我正在使用 MVC 方法(不使用任何框架,纯 PHP)在 PHP 中开发 Web 应用程序。与 MVC 的情况一样,每个请求都到达前端控制器,前端控制器将其路由到相应的控制器,并执行请求的操作。URL 结构如下所示:

假设我正在构建一个电子商务网站,其中包含不同类别的产品。可能的 URL 可能是:

对于第一个 URL,加载“沙发”控制器,并执行它的 overview() 方法。这一切都很好,直到我们必须将这些产品嵌套在父类别中。我将使用前两个 URL 来说明我的意思:

现在,“视频游戏”控制器嵌套在“电子”控制器中。但是,对于当前的“加载控制器 -> 执行操作”结构,这将不起作用。

一个可能的解决方案是在父控制器(“电子设备”)中创建一个方法,在请求不存在的操作(“视频游戏”)的情况下执行该方法。此方法检查请求的操作是否作为控制器存在。如果是这样,控制器将被加载并执行它的操作(“概述”)。

我徒劳地寻找解决标准前端控制器模式限制的解决方案,包括SO 上。我认为我现在对 MVC 的实现是正确的,但是前端控制器仍然带来了限制。