对MVC、PHP的流程感到困惑

发布于 2024-12-05 17:19:48 字数 908 浏览 0 评论 0原文

我一直在尝试了解 MVC,但我对控制器和模型的输入流程有一些疑问。

  1. 假设用户访问 example.com 并进入其主页。然后他们选择 search.php 链接。 controller.php如何知道用户请求的是search.php而不是user.php?
  2. 当controller.php知道search.php已被选择时,它将加载模型,然后加载view.php。但是当调用这些时,代码将如下所示。

    类搜索扩展了 Core_Search_Controller
    
    公共函数 inboxSearch(){
      $this->view->navigation = $this->navigation();
      $this->load->box = $this->box();
    }
    

没有文件夹或类视图,也没有用于加载的文件夹或用于加载的类。我可以在不同的文件中找到功能导航,但其文件夹位于不同的位置。它如何在不包含或要求的情况下访问该文件?

  1. 一旦search.php的controller.php如何知道search.php请求了信息呢?也许这对于第一个问题来说是多余的,但我对此很困惑。

我知道它很长,对此感到抱歉。

*编辑:*根据我从我所在的项目中了解到的是,控制器中的所有函数末尾都有 Action ,将指向具有相应名称的视图。例如index.php/.tpl

 class IndexController extends Zend_Controller_Action{

    public function indexAction(){
    /**

    Somecode
    **/

    }

}

干杯

I have been trying to learn about MVC but i have a few questions about the flow of input to the controller and then to the model.

  1. Say a user goes to example.com and get to their home page. They then select the search.php link. How does the controller.php know that the user has requested the search.php instead of user.php?
  2. When the controller.php knows the search.php has been selected it will load the model and then the view.php. But when calling these the code will look like this.

    class Search extends Core_Search_Controller
    
    public function inboxSearch(){
      $this->view->navigation = $this->navigation();
      $this->load->box = $this->box();
    }
    

There is no folder or class view and no folder for load or class for load. And I can find function navigation in a different file but its folder is different location. How can it access that file without include or require?

  1. Once of search.php How does the controller.php know that search.php has requested information? Maybe this is redundant from question number one but im quite confused on this.

I know its long, sorry about that.

*Edit:*From what i have learned from the project i am on is that all the functions in the controller have Action on the end of them will direct to a view with the corresponding name. such as index.php/.tpl

 class IndexController extends Zend_Controller_Action{

    public function indexAction(){
    /**

    Somecode
    **/

    }

}

Cheers

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

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

发布评论

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

评论(1

梦一生花开无言 2024-12-12 17:19:48

大多数 MVC 框架为您做了很多幕后魔法,这可能会让您对事情的工作原理感到困惑。

为了回答您的第一个问题,大多数框架使用带有重写规则的 .htaccess 文件,该规则会将所有流量重定向到您的控制器。因此,当您请求 search.php 时,它实际上会调用控制器而不是 search.php。从那里,控制器可以查看您最初请求的内容(在本例中为 search.php),以找出适当的模型和视图。

我相信你的第二个问题的答案是它会根据需要自动加载文件。这是另一个神奇之处,它可以查看类名并找出文件的位置并加载它。您可以在 PHP 手册中阅读更多相关内容。
http://php.net/manual/en/language.oop5.autoload.php

为了回答你的最后一个问题,获取请求的信息通常由控制器在查看请求时处理。例如,如果您请求“example.com/blog/7263”,它会判断您想要“blog”模型,并且 id 号为 7263。根据您使用的框架,配置此方式的方式会有所不同。

我希望这会有所帮助。

Most MVC frameworks do a lot of behind the scenes magic for you and this is probably what has you confused about how things work.

To answer your first question, most frameworks use a .htaccess file with a rewrite rule that will redirect all traffic to your controller. So, when you request search.php, it will actually call the controller and not search.php. From there, the controller can look at what you originally requested (search.php in this case) to figure out the appropriate model and view.

I believe the answer to your second question is that it is auto-loading the files as needed. This is another bit of magic, where it can look at the class name and figure out the location of the file and load it. You can read more about this in the PHP manual.
http://php.net/manual/en/language.oop5.autoload.php

To answer your last question, getting the requested information is often handled by the controller when it looks at the request. For example, if you request "example.com/blog/7263", it will figure out that you want the "blog" model and that the id number is 7263. Depending on what framework you are using, the way that you configure this will be different.

I hope that helps a little.

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