Autloader在MVC中应该放在哪里? [英] Where should autloader go in MVC?

查看:107
本文介绍了Autloader在MVC中应该放在哪里?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试构建一个简单的MVC框架,以更好地理解某些概念.我认为要解决的第一件事是前端控制器,它可以处理对我的应用程序的所有请求.

I'm trying to build a simple MVC framework to better understand certain concepts. The first thing that I thought would be important to address is a front controller that handles all of the requests for my applications.

一旦我开始考虑,我不确定装入应用程序将要使用的类的最佳方法.我当前的想法是,由于每个请求都使用自动装带器,因此我的自动装带器应位于前端控制器中.大多数框架通常将其放置在哪里?浏览一些已经建立的框架并没有太大帮助,因为很多功能远远超出了我的需求,使它变得非常复杂,以至于难以理解.

Once I started to think about it, I wasn't sure of the best way to load the classes that my application would be using. My current thought process is that my autoloader should be located in the front controller since every request utilizes it. Where do most frameworks typically put this? Looking through a few already built frameworks hasn't helped me much as a lot of the functionality far exceeds what I need, complicating it so much that it's hard to understand.

我要使用的类加载器可以在这里找到 https://gist.github.com/221634

The class loader that I am trying to use can be found here https://gist.github.com/221634

只是试图弄清楚如何适当地构建和组织一个简单的MVC框架.

Just trying to figure out how to appropriately build and organize a simple MVC framework.

推荐答案

您应将其放入引导文件中.

You should put it in your bootstrap file.

这是您可以执行的操作:

This is how you can do this:

  1. 将每个HTTP请求强制发送到前端控制器index.php,app.php或调用方式.
  2. Front Controller可以定义框架中使用的一些常量,然后包含Bootstrap.php文件. Bootstrap将启动您的应用程序.
  3. 现在,我在Bootstrap中要做的第一件事是注册自动加载.这样,我可以轻松地获得\ System \ Router \ Router类或\ System \ Router \ Dispatcher类,您就明白了.

还有一件事情,您甚至可以使用PSR0类加载器注册您的应用程序Models文件夹. 因此,可以说您的Models文件夹如下所示:

One more thing, you can even register your application Models folder with PSR0 class loader. So lets say that your Models folder looks like this:

application/Models/
    - Entities
    - Services
        Email.php
        Cache.php

从控制器内部,您可以轻松获得这样的模型

From inside your controller you can easily get Models like this

public function someController()
{
    $email = new \Models\Services\Email();
    // Do stuff with email service
}     

对于您的问题的简短回答是,最好的东西是首先给您一些摆动"空间的Front Controller,然后从那里加载引导您的应用程序的Bootstrap,Bootstrap中的第一件事是要求您的类加载器,并注册要通过应用程序使用的库.

So short answer to your question is that the best thing to have is first Front Controller that gives you some "wiggle" room, then from there you load your Bootstrap that boots up your app, and first thing in Bootstrap is to require your class loader, and register libraries you want to use through application.

然后您甚至可以为应用程序的Controllers和Models文件夹注册自动加载,并且在要分派请求的Bootstrap文件末尾,您可以像下面这样请求Controller:

And then you can even register autoloading for your application Controllers and Models folder, and at the end of Bootstrap file when you are about to dispatch request you can ask for Controller like this:

$app = new '\\Application\\Controllers\\' . $class;
// Dispatch request with call_user_func_array or ReflectionMethod and ReflectionClass

由于Controller类是自动加载的,因此不需要它,只需为其提供正确的名称空间即可.

No need to require Controller class since its autoloaded, just provide it with correct namespace.

好问题,希望对您有所帮助!很高兴看到还有其他人在玩他们的自定义MVC:)

Great question, hope this helps! Nice to see there are other guys playing around with their custome MVC :)

这篇关于Autloader在MVC中应该放在哪里?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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