Where should the autoloader be in MVC?

I am trying to create a simple MVC structure to better understand some concepts. The first thing that, in my opinion, would be important for the solution is the front controller, which processes all requests for my applications.

As soon as I started thinking about it, I was not sure that you could download the classes that my application would use. My current process is that my autoloader must be located on the front controller, as each request uses it. Where, by and large, are most of the frameworks? Examining a few frameworks already built did not help me, since many functions far exceed what I need, which complicates it so much that it is difficult to understand.

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

Just trying to figure out how to properly build and organize a simple MVC infrastructure.

+4
source share
3 answers

You must put it in your bootstrap file.

Here's how you can do it:

  • Configure each HTTP request to your front controller, index.php, app.php or whatever you want to call it.
  • Front Controller can define some constants used in the framework and then include your Bootstrap.php file. Bootstrap will load your application.
  • Now the first thing I do in Bootstrap is to register autoload. That way, I can easily get the class \ System \ Router \ Router or \ System \ Router \ Dispatcher, you get the point.

One more thing: you can even register the Application Models folder with the PSR0 class loader. So let's say that the 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, the short answer to your question is that it is best to have the first front controller, which gives you some kind of "wiggle" room, and then from there you download your Bootstrap, which loads your application, and, first of all, in Bootstrap requires your classloader and register library, which you want to use through the application.

And then you can even register autoload for your folder “Controllers and application models”, and at the end of the Bootstrap file, when you are going to send a request, you can request the controller as follows:

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

You do not need to require a controller class from the time it is started, just specify the correct namespace for it.

Great question, hope this helps! It's nice to see that there are other guys who play with their Custome MVC :)

+3
source

Definitely in the boot phase!

The autoloader must be part of every PHP application, and it must be (one of) initialized with the first class / code.

My MVC initialization steps:

  • index is an entry point
  • bootstrap to initialize error handling, autoloader and IoC
  • mostly MVC application
  • routing mechanism
  • controller
  • model
  • view
+2
source

Well, the question is "Where is he going?" I am asked two more exact questions:

  • Where is the file containing the autoloader function / class definition stored?
  • Where in your request should the send cycle be created, configured and allowed to perform its magic?

The first question is "Where to place the file containing the class?" - Perhaps this is not so important for you, because you have defined the autoloader class that you want to use. The exact answer depends on your own application structure structure, but for an externally designed class like the one you are quoting, maybe something in the lib or vendor directory makes sense.

For the second question - "Where to create, configure, etc."? - answer: as soon as possible in the query loop so that you can take advantage of autoload for all classes referenced later. In practical terms, this probably means that you are somewhere in the boot process.

Of course, this usually means that to load your autoloader class, you probably have to do a manual call to require / include , create an instance of your autoload object, and configure it using namespaces and paths.

+1
source

Source: https://habr.com/ru/post/1440627/


All Articles