E
E
Edward2016-07-31 22:13:40
PHP
Edward, 2016-07-31 22:13:40

How to use controllers and models in frameworks?

I'm trying the Slim framework now, how can I use models there? How is it generally arranged in frameworks?
1) For me, this construction is incomprehensible:

$app->get('/hello/{name}', function (Request $request, Response $response) {
    $name = $request->getAttribute('name');
    $response->getBody()->write("Hello, $name");

    return $response;
});

namely function (Request $request, Response $response)
2) I can not fully understand how to use the models, where they are stored and how to use them.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2016-07-31
Protko @Fesor

model is a very abstract word. Let's look at the dictionary:
So the "model" in the context of MV* is... the model of your application, i.e. what the application actually does. The main thing is not to be mistaken that this is a "way to access data", in fact everything that relates to data processing is there. That is, controllers can at most ask the model to do something or give a representation of some data that the controller would form from this view (HTTP response in the context of the PHP framework).
There is also such a thing - Action-Domain-Responder is called, there are a little less "abstract words" but the essence is about the same as that of MVC.
But back to the question
It's up to you. Slim is a very simple HTTP framework. Its job is to give you the controllers, and the model is your job to implement it. You can arm yourself with a dependency container or even a service locator, and pull the necessary services that will already do things.
The point of all this is to abstract the "model" i.e. your application from HTTP. So that you can then reuse your application code with other interfaces (usually console commands or queues).
You have already been given a link to "why dependency injection is needed", I will just add more or less useful reading on the topic of separation of concerns and layered architectures in slim: codereview.stackexchange.com/questions/93914/slim-...

A
Alexander Aksentiev, 2016-07-31
@Sanasol

www.phptherightway.com/#dependency_injection

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question