问题描述
对于PHP MVC应用程序,index.php文件和前控制器的作业有什么区别?是index.php中的前控制器还是在单独的文件中?我该如何分开两者并让他们一起工作?前控制器应该是一类(还是像自己的实体)? (如果是这样,那么index.php将实例化前控制器?)
我知道他们必须"设置环境",其中包括定义一些常数等,但是什么? ( - 自动加载,调试物等)
我已经看到了: MVC带有前控制器混淆,但是,但是,但是这并不能解决index.php和前控制器之间差异的问题.
推荐答案
实际上,index.php根本不应包含任何有意义的代码,因为它只是网站的一部分,位于Web服务器的DOCUMENT_ROOT内.它的内容实际上应该看起来像:
<?php require '../application/bootstrap.php';
它应该仅包括DOCUMENT_ROOT外部的文件.仅此而已.
这样,如果出现了可怕的错误(例如,PHP扩展程序在服务器更新后失败),并且访问者接触了原始的PHP代码,它将不会透露任何敏感的细节.
front Controller 是处理所有用户输入,将其变成a dobable 表单,并基于它,派遣命令(通常以对象的方法调用形式).在诸如Java之类的语言中,所有内容都必须包含在类中,前控制器将是一类.但是在PHP中,您没有这种限制.
而不是前控制器最终将成为应用程序的引导阶段的一部分:
// --- 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启发的体系结构的应用程序.
其他推荐答案
index.php应初始化应用程序,并调用将路线解码为控制器和操作的东西,并运行它们.看看Yii,Symfony,Codeigniter,CakePHP,看看他们的工作.所有略有不同,但原理相同.
yii index.php的一个示例来指出:
<?php $yii=dirname(__FILE__).'/../../framework/yii.php'; $config=dirname(__FILE__).'/protected/config/main.php'; require_once($yii); Yii::createWebApplication($config)->run();
$ config将传递给Web应用程序,该应用程序用作前控制器.
其他推荐答案
您确实应该阅读MVC的结构,特别是与PHP一起使用.在index.php中初始化一个前控制器的实例,如果该过程是前控制器初始化过程的一部分(__constructor()).
,则应渲染您的页面.问题描述
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.)
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.
推荐答案
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';
It should only include a file outside DOCUMENT_ROOT. And that's all.
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.
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.
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
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.
其他推荐答案
Index.php should initialize the application and call something that deciphers the route into controller and action, and runs them. Look at Yii, Symfony, CodeIgniter, CakePHP, see what they do. All slightly different but same principle.
An example from Yii's index.php to make the point:
<?php $yii=dirname(__FILE__).'/../../framework/yii.php'; $config=dirname(__FILE__).'/protected/config/main.php'; require_once($yii); Yii::createWebApplication($config)->run();
$config gets passed to the web application, which serves as the front controller.
其他推荐答案
You really should read up on the structure of MVC, specifically when used with PHP. Initialize an instance of front-controller in index.php, and it should render your page if that process is part of the front-controller initialization procedure (__constructor()).