"适当" index.php和前端控制器之间的分离/区别 [英] "Proper" separation/difference between index.php and front controller

查看:81
本文介绍了"适当" index.php和前端控制器之间的分离/区别的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

对于PHP MVC应用程序, index.php 文件和前端控制器的作业有什么区别?前端控制器在 index.php 中,还是在单独的文件中?我如何分开两者,让他们一起工作?前台控制器是否应该是一个类(或者像自己的实体)? (如果是这样,那么index.php会实例化前端控制器?)

For a PHP MVC application, what is the difference of the job of the index.php file and front-controller? Is the front-controller in the index.php, or is it in a separate file? How do I separate the two and let them work together? Is the front-controller supposed to be a class (or like its own entity)? (If that's the case, then index.php will instantiate the front-controller?)

我知道他们必须设置环境,其中包括定义一些常数等,但是做什么呢? ( - 自动加载程序,调试内容等)

I know that they have to "set up the environment," which includes defining some constants and etc, but what does what? (-- autoloader, debug stuff, etc.)

我已经看到: MVC与前端控制器混淆,但这并不能解决 index.php 之间的区别的问题和前端控制器。

I have seen this: MVC with a front controller confusion, but that doesn't solve the problem of the difference between index.php and the front-controller.

推荐答案

实际上, index.php 应该不包含任何有意义的代码,因为它将只是您的网站的一部分,位于网络服务器的 DOCUMENT_ROOT 内。它的内容实际上应该是这样的:

Actually, index.php should not contain any meaningful code at all, since it would be only part of your site, that is located inside DOCUMENT_ROOT of webserver. It's content should actually look something like:

<?php 

    require '../application/bootstrap.php';

它应该只包含 DOCUMENT_ROOT 。这就是所有的。

It should only include a file outside DOCUMENT_ROOT. And that's all.

这样,如果有些事情出现了可怕的错误(比如,在服务器更新后,php扩展失败),访问者暴露于原始的PHP代码,它不会显示任何敏感细节。

This way, if something goes horribly wrong (like, php extension fails after server update) and visitors are exposed to raw php code, it will not reveal any sensitive details.

前端控制器 处理所有用户输入,将其转换为可消耗的表单,并基于此,分派命令(通常以对象的方法调用形式)。在Java语言中,必须将所有内容都包含在类中,前端控制器将是一个类。但是在php中没有这个限制。

The point of Front Controller is handle all user input, turn it into a consumable form and, based on it, dispatch a command (usually in a form of method call on an object). In languages like Java, where everything must be contained in a class, a front controller would be a class. But in php you do not have this restriction.

相反,前端控制器将最终成为应用程序启动阶段的一部分: / p>

Instead the front controller will end up being part of your bootstrap stage of the application:

// --- snip --- 
// the autoloader has been initialized already a bit earlier

$router = new Router;
$router->loadConfig($configuration);

$request = new Request;
$request->setUri($GET['url']); 
// could also be $_SERVER['PATH_INFO'] or other
// depends on how url rewrite is set up

$router->route($request);
// the request instance is populated with data from first matching route

$class = $request->getParameter('resource');
$command = $request->getMethod() . $request->getParameter('action');

if (class_exists($class)) {
    $instance = new $class;
    $instance->{$command}($request);
    // you dispatch to the proper class's method 
}

// --- snip --- 
// then there will be some other code, unrelated to front controller

此外,您应该记住前端控制器的概念既不是试图实现MVC或MVC启发式架构的应用程序,也不是要求的。

Also, you should keep in mind that concept of front controller is neither made-for nor demanded-by application that attempt to implement MVC or MVC-inspired architecture.

这篇关于&QUOT;适当&QUOT; index.php和前端控制器之间的分离/区别的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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