PHP MVC framework where you want to place your own classes

I just started looking at the MVC pattern. My question is:

Where would I put other class files (Database, User, Logger, Mailer, etc.)? Should I create a new directory for them, for example. libs ?

Should I create classes in Controller inside model function?

 <?php class Controller { protected function model($model) { require_once('../app/models/'. $model .'.php'); return new $model(); } protected function view($view, $data = []) { require_once '../app/views/'. $view .'.php'; } } 
+5
source share
2 answers

Where would I put other class files (Database, User, Logger, Mailer, etc.)? Should I create a new directory for them, for example. libs ?

Putting them in separate files, since they all provide different functionality, should be fine. There is no difference in your name of directories - if it corresponds to the naming conventions of your project or in general (perhaps even better).

Should I create instances in the controller inside the model function?

Not. As far as I can see, the stream may look like:

  • The index file receives the request and initiates a new bootstrap instance
  • bootstrap installs a custom handler and router
  • router then calls the appropriate method based on the request method and uri provided by matching with the set of routes.
  • The corresponding route initializes all components of the MVC triad and the called method. Components ( Model layer, View layer and Controller layer) are passed to a method called router . In my case, I call the FrontController class, the init method.
  • Basically, init is where the MVC triad actually runs. The Model level is responsible for business logic, persistence, etc. It is important to note that Model is not one file or class (the same for View and Controller ). The View and Controller levels access the Model layer to perform the appropriate actions. View The task of the layer is to control the output, for example, to decide whether the output is Content-Type application/json or text/plain , or which Template to render. In addition, Views not Templates that are designed to display data. Notice Views request the necessary data directly from the Model layer; no interaction with the Controller level. Finally, Controller layers are executed when there is a need for interaction, for example, the user submits a form, the corresponding Controller filters the input and calls the method from the Model layer.
+2
source

Since MVC has three main parts, I would recommend (and notice that almost all the structures on the market do this) to create a directory of each of the three components and put the classes in the corresponding directory.

As for other components, Database is a utility and can be placed, for example, in the lib directory, User is a model and can go to the model folder and Logger / Mailer can also go to the lib folder. These are examples, not something to strictly follow.

As for the instance, each Controller can define a list of models and libraries , it depends on how the MVC structure handles the initialization of these objects. Therefore, you should follow the dependency injection pattern.

+1
source

All Articles