C
C
cryp242019-03-20 18:06:02
Design patterns
cryp24, 2019-03-20 18:06:02

How to write under MVC on laravel example?

I can’t figure out how to write under MVC correctly, in particular, problems with understanding what to shove into the controller and what into the model, with the view it’s more or less clear, please tell me using my example because I’ve already read the theory.
So there is a news parser, at the moment it looks like this: I
created a model in App, I created a function in it (the code is large, so I’ll just write it in blocks 1-2-3-4-5)

public static function pars() {

1) Парсим страницу + распарсиваем нужные данные. На выходе имеем массив спарсинных данных для вставки в бд.
2) Сверяемся с базой на наличие дублей и формируем новый массив для вставки без дублей. На выходе имеем массив уникальных новостей.
3) Далее идет вставка этого массива в бд
4) После поступления новых данных, идет кое какая проверка по новым новостям на основании которой, удаляются некоторые старые.
5) Затем идет выборка по определенным критериям из этих новостей и отправка мне смс, если такие есть

return;
}

respectively in web.php
Route::get('/pars', function () {

    App\News::pars();
    return ;
});

At the output, I have a site / pars parser that is screwed onto the cron and everything is ready.
As you noticed, there is no controller in this circuit. How to correctly distribute the code from my example so that it matches the MVC model?

Answer the question

In order to leave comments, you need to log in

2 answer(s)
A
Alexander Aksentiev, 2019-03-20
@Sanasol

At the output, I have a site / pars parser that is screwed onto the cron and everything is ready.

And everything is ready as crookedly as possible, and not as we would like https://laravel.com/docs/5.8/scheduling
And the controller is not needed here at all, and pulling the MVC abbreviation on anything.
And a method in the model that does something too, although the model itself is not used there at all https://laravel.com/docs/5.8/providers
And the all-in-one parsing itself https://laravel.com/docs/5.8 /queues

R
Roman, 2019-03-20
@procode

Judging by the code that I saw here and there in Laravel, it is customary to shove logic not into the model, but into the controller :)
The model is usually used to interact with the database and set up relationships.

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question