Where should autloader go in MVC?

冷暖自知 提交于 2019-12-05 01:25:20

问题


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.

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

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


回答1:


You should put it in your bootstrap file.

This is how you can do this:

  1. Force every HTTP request to your front controller, index.php, app.php or how ever you want to call it.
  2. Front Controller can define some constants used in framework and then include your Bootstrap.php file. Bootstrap will boot up your application.
  3. Now, first thing in I do in Bootstrap is to register autoloading. This way I can easily get \System\Router\Router class, or \System\Router\Dispatcher class, you get the point.

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
}     

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.

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

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

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




回答2:


Definitely in the bootstrap stage!

The autoloader should be part of every PHP application and it should be (one of) the first classes/code initialized.

My MVC initialization steps:

  • index is the entry point
  • bootstrap for initializing error handling, autoloader and IoC
  • application, mostly a MVC app
  • routing mechanism
  • controller
  • model
  • view



回答3:


Well, the question "Where does it go?" to me suggests two more precise questions:

  1. Where is the file stored that contains the autoloader function/class definition?
  2. Where in your request dispatch cycle should it be instantiated, configured, and allowed to do its magic?

The first question - "Where to place the file containing the class?" - is probably not so critical for you since you have identified an autoloader class you want to use. The precise answer depends upon your own framework app structure, but for an externally developed class like the one you cite, someplace within a lib or vendor directory probably makes sense.

For the second question - "Where to instantiate, configure, etc?" - the answer is: as early as possible in the request cycle, so you can get the benefit of autoloading for all classes that are referenced later. In practical terms, that probably means somewhere in your bootstrap process.

Of course, this usually means that in order to load your autoloader class, you will probably have to do a manual require/include call, instantiate your autoloader object, and configure it with with namespaces and paths.



来源:https://stackoverflow.com/questions/12961422/where-should-autloader-go-in-mvc

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!