PHP:代码设计困境

发布于 2024-10-03 16:50:41 字数 1358 浏览 4 评论 0原文

这是一个没有真正问题的问题,只是我病态思想的产物,并且让事情变得有点奇怪:)

所以,我在我自己的面向 MVC 的框架之上构建了这个 PHP 应用程序(是的,我做了我自己的框架)使用现有的)。这是按书本完成的,因此我们有模型(数据和数据库操作)、视图(填充数据和渲染输出的模板)和控制器(处理请求、从模型获取适当的数据、将数据放入视图中)。使用 .htaccess 规则完成请求路由的经典而无聊的场景。

昨天我对我的代码做了一些更改,修复了错误,进行了一些改进等。我强烈要求重新安排控制器的代码。它们感觉有些沉重和臃肿,方法的数量使得浏览文件等变得很困难。我相信每个人都知道我在说什么。

我正在考虑将我的控制器类分成许多类,每个类仅处理一种类型的请求,例如登录或注册或 showProfile 或killMe。

现在控制器类具有与用户友好(或者可能是 SEO 友好)url 部分相对应的公共方法,并且路由类根据 url 内容调用适当的控制器及其方法。

我正在考虑的更改会将一些路由机制转变为调用特定控制器及其 Execute() 方法。

例如,对于 url =“www.example.com/users/login” 现在看起来像这样:

$controller = new url[0]();
$method = url[1];
echo $controller->$method();

现在 url 将更改为“www.example.com/login”,路由代码将如下所示:

$controller = new url[0]();
controller->Execute();

我省略了解析 url 并从中提取路由信息的部分,因为它与我的问题无关。

我从这一变化中看到了什么好处?

  • 每个请求一个专用类
  • 更小的文件
  • 更小的代码
  • 更容易维护
  • 在添加新功能(新类型的请求)或修复错误时,破坏工作控制器的危险有限

缺点?

  • 可能很多课程
  • 可能会影响性能
  • ???

我的问题是你对这个想法有何看法,它是否有意义。当然,我更感兴趣的是为什么我不应该这样做,而不是为什么我应该这样做。因此,如果您能想到任何理由说明为什么这是一个糟糕的想法和令人厌恶的想法,请立即发言,以免为时已晚:)

已编辑 澄清一个问题:

我问是否应该将通过其方法处理多种类型请求的单个大控制器分解为许多小控制器,每个控制器仅处理单一类型的请求。

现在我有控制器用户来处理“登录”、“showLoginForm”、“注册”、“激活”等请求。重构的代码将包含每个请求的单独控制器。

This is a question with no real problem behind, just a product of my sick mind and drive to make things slightly weird :)

So, I have this PHP application build on top of my own MVC oriented framework (yes, I did my own instead of using existing one). And it is done by the book so we have model (data and database manipulation), view (templates filled with data and rendering output) and controller (handles requests, gets appropriate data from model, puts data into view). Classic and boring scenario with request routing done with .htaccess rules.

Yesterday I did some changes in my code, bug fixes, couple improvements, etc. And I felt strong urge to rearrange the code of controllers. They feel somewhat heavy and bloated and number of methods makes it hard to navigate through the file, and such stuff. I'm sure everybody knows what I'm talking about.

I'm thinking about breaking my controller class into many classes, each one handling only one type of request like login or register or showProfile or killMe.

Now controller class has public methods corresponding to parts of user friendly (or maybe SEO friendly) urls and routing class invokes proper controller and it's method according to url content.

Change I'm thinking about would shift a little routing mechanism into invoking specific controller and it's Execute() method.

For example, for url = "www.example.com/users/login"
now it looks like that:

$controller = new url[0]();
$method = url[1];
echo $controller->$method();

and now url would change to "www.example.com/login" and routing code would look like that:

$controller = new url[0]();
controller->Execute();

I omitted parts where I parse urls and extract routing info from them as it is irrelevant to my question.

What benefits I see in that change?

  • one dedicated class per one request
  • smaller files
  • smaller code
  • easier maintenance
  • limited danger of breaking working controller when adding new features(new types of request) or fixing bugs

Disadvantages?

  • possibly a lot of classes
  • possible performance hit
  • ???

And my question is about what do you think about that idea and does it make any sense at all. And of course I'm more interested why I shouldn't do it than why I should. So if you can think of any reasons why that would be terrible idea and abomination please speak now before it will be too late :)

EDITED
Clarification of a question:

I'm asking whether I should break my single big controller which handles many types of requests by it's methods into many small controllers each of them handling only single type of request.

Now I have controller Users which handles requests like "login", "showLoginForm", "register", "activate", etc. Refactored code would consist of separate controllers for each of these requests.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

箹锭⒈辈孓 2024-10-10 16:50:41

我能想到的新旧方法的一个缺点是,您将 url 直接映射到类名。如果您想更改 url,则必须更改类名。如果您想为不同的语言使用不同的 url,则必须添加一个层来将 url 映射到类名。
这就是为什么我宁愿有一个路由类将 url 映射到类名,这为您提供了改变事物的机会。

A disadvantage I can think of for both the old and new methods is that you are mapping urls directly to class names. If you'd like to change the url you'd have to change the class name. If you'd like to have different urls for different languages, you'll have to add a layer that will map urls to class names anyways.
This is why I'd rather have a routing class that will map urls to class names which provides you a seam to change things.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文