通过 URL 指定 Controller 类与为每个 Controller 指定脚本的优缺点是什么? [英] What are the pros and cons of specifying a Controller class via URL vs. having a script for each Controller?

查看:59
本文介绍了通过 URL 指定 Controller 类与为每个 Controller 指定脚本的优缺点是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

今年夏天我设置了两个不同的 PHP 系统.每个都使用两种不同的方法:

I have setup two different PHP systems this summer. Each uses two different methods:

方法 #1:每个任务一个 PHP 文件

此方法要求为每个主要任务创建一个 PHP 文件.例如,我的上传脚本可以通过 http://www.domain.com/upload.php 访问.在 upload.php 上,实例化并使用了控制器"和视图"类.例如,upload.php 可能如下所示:

This method requires that a PHP file be created for each major task. For example, my upload script would be access via http://www.domain.com/upload.php. On upload.php, a "controller" and a "view" class are instantiated and used. For example, upload.php might look something like this:

<?php
require_once PATH_LIBRARY . 'control/Uploader.class.php';
require_once PATH_LIBRARY . 'view/UploaderPage.class.php';

$uploader = new Uploader();
$uploader->setView(new UploaderPage());
$uploader->init();
?>

在上面的脚本中,如果我想调用另一个脚本,我只需重定向并附加必要的 URL 编码变量 (header('Location: edit_details.php?image_id=456');).

In the above script, if I wanted to call on another script, I would simply redirect and append the neccessary URL encoded variables (header('Location: edit_details.php?image_id=456');).

方法 #2:处理所有请求的主 index.php 文件

Method #2: A main index.php file that handles all requests

此方法不需要为每个主要任务创建一个 PHP 文件.相反,将在系统中使用的每个控制器"类都在 index.php 上注册了一个我称之为 Router 的唯一类.路由器决定指定的控制器是合法的还是非法的并采取相应的行动(将浏览器路由到正确的控制器).例如,index.php 脚本的简化版本:

This method does not require that a PHP file be created for each major task. Instead, each "controller" class that will be used in the system is registered on index.php with a unique class that I call Router. The router decides if the specified Controller is legal or illegal and acts accordingly (routes the browser to the correct controller). For example, a simplified version of the index.php script:

<?php
require_once 'bootstrap.inc';
require_once PATH_LIBRARY . 'router/Router.class.php';

$router = new Router();
$router->register('Uploader', PATH_LIBRARY . 'control/Uploader.class.php');
$router->register('DetailsEditor', PATH_LIBRARY . 'control/DetailsEditor.class.php');
$router->route();
?>

因此,每个操作都在 index.php 中进行.不需要很多文件,这些文件不会做太多其他实例化特定视图和控制器类的工作.但是,如果您想从脚本/类 B 中调用脚本/类 A,那么您需要在 URL 上传递控制器类的名称: header('Location: index.php?controller=DetailsEditor&image_id=456').

Thus, every action takes place at index.php. There is no need for a lot of files which don't do much else that instantiate a specific view and controller class. BUT, if you want to call script/class A from script/class B, then you need to pass the controller class's name along on the URL: header('Location: index.php?controller=DetailsEditor&image_id=456').

到目前为止,我不太喜欢需要在 URL 中包含控制器名称的事实.我觉得它向最终用户暴露了太多我的底层系统.但是,我确实喜欢我可以在一页上注册所有控制器类的事实.大多数情况下,我不知道公开控制器名称是否安全.另一个烦恼是,如果我想通过 POST 请求调用脚本,我必须包含一个指定所需控制器类的隐藏输入(例如 <input type="hidden" name="controller" value="DetailsEditor"/>).

So far, I don't really like the fact that I need to include the Controller name in the URL. I feel that it exposes too much of my underlying system to the end-user. But, I do like the fact that I can register all controller classes on one page. Mostly, I don't know if exposing the controller name is safe or not. One other annoyance is that, if I want to call on a script via POST requests, I have to include a hidden input that specifies the required controller class (e.g. <input type="hidden" name="controller" value="DetailsEditor" />).

我希望这足以继续下去.我只是担心第二种方法在接下来的几个月里不会真正对我有用.我很快就会有一小段时间来选择其中之一.

I hope that is enough to go on. I am just nervous that the second method is not really going to serve me well in the months ahead. I have a small window of time soon to choose one or the other.

谢谢.

推荐答案

没那么快. 每个动作一个 PHP 文件的方法可以更快更好.这取决于您如何设置.

Not so fast. The approach with one PHP file per action can be faster and better. It depends on how you set things up.

  • Web 服务器将根据文件名将 HTTP 请求路由到正确的处理程序.为什么要重新发明它?

  • The web server will route the HTTP request to the correct handler based on the filename. Why reinvent that?

大多数情况下,处理程序"只需要:

Most times, all you need for a "handler" is:

<?php include "everything.php";
// do some work
// Bounce the browser to a "view" url or display an error.

至少同样简洁.

这篇关于通过 URL 指定 Controller 类与为每个 Controller 指定脚本的优缺点是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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