M
M
Mors Clamor2020-11-22 15:17:54
PHP
Mors Clamor, 2020-11-22 15:17:54

How to properly connect Controller and Model in MVC?

Hello! In the process of writing the bot, VK came to the MVC structure, largely looking back at Yii2, so the question arose, how to connect all the components to each other? For example, the request hits the index page, where an instance of the App application is launched, which, in turn, calls within itself the same method corresponding to the type of Callback event (for example, the message_new method). The App class contains the necessary classes and configs inside. Since my application is based on message-commands, in the message_new method I analyze what has come there and already start classic routing, that is, I look for the controller and call it, and here the problem is: how to pass the controller, and the model an instance of the class ? In Yii2, this is implemented, as I understand it, by a global container with a static $app property, but I read that global access is bad.

How is it right to do this, what ways to solve this problem exist? And will it be better if I implement the same global static class as a Singleton?

Answer the question

In order to leave comments, you need to log in

1 answer(s)
D
Decadal, 2020-11-23
@66demon666

MVC is about the idea, not about the implementation.
You can implement via a global container if the application is not expected to be developed and maintained by a large team. You can stubbornly make a flexible, well-thought-out orchestration configuration with controllers, models and views and get your own version of Symphony. The technical correctness of decisions is inferior to the expediency of their implementation.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question