gpt4 book ai didi

php - Controller 和路由器的区别?

转载 作者:行者123 更新时间:2023-12-03 19:12:00 25 4
gpt4 key购买 nike

我目前正在整理一个小的 mvc 框架用于实践,我有一些 Laravel 经验,所以它大致基于此。我制作了一个路由器,它只是为您设置的 url 返回一个指定的 View 。

现在我还制作了分割 url 的 Controller ,并使用基本 url 之后的第一部分作为 Controller ,第二部分作为 Action 。这将加载与 Controller 对应的文件和该文件中与操作对应的方法。

因此,如果 url 类似于:url.com/users/index,它将加载一个 UsersController.php 文件并在该文件中查找 index() 方法。

现在我想知道 Controller 和路由器之间的确切区别是什么?它像一个指定的?其中路由器更简单一点,只是对给定的确切网址使用react,而路由器将其切碎并具有更多深度?

我目前所拥有的似乎有些重叠。

最佳答案

(假设路由器不是指网络硬件或木工工具!)

路由器接受请求并决定哪些 Controller / Controller 方法将处理请求。

Controller 接受请求并处理它!

Now I've also made controller that splits the url and uses the first part after the base url as controller and second part as action. This loads a file corresponding with the controller and a method within that file corresponding with the action.



这并不是真正的 Controller (就 MVC 而言),它是路由的一部分。

例如取 [GET] uri:example.com/article/view/123
MVC 路由器将解析 uri 并找到以下段
  • 文章
  • 查看
  • 123

  • 默认情况下,大多数路由器现在会实例化 articleController并调用其 view传入的方法 123作为参数。 (您也可以使用一些 getUriSegment(segmentIdx) 方法,这是您的框架的设计选择。)
    ArticleController会有一个 view带有 $ articleId 的方法范围。此方法可能会执行以下操作:获取指定的文章(例如,通过模型从数据库中获取)然后显示它(可能通过返回已给定模型返回的文章的 View )

    关于php - Controller 和路由器的区别?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27933802/

    25 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com