How to better design my application - Service layer

Right in the main.php, container section: https://www.yiiframework.com/doc/guide/2.0/en/concept-configurations#application-configurations

1 Like

I mean inside the array of components?

No. Inside container.

1 Like

Sorry my ignorance Alexander, since we are showing you two points,

The first one on the subject that we talked about and I am too grateful, as you mention then within this configuration array, should I create a new element called ‘container’, and there declare what I need? Since an element named like this doesn’t appear, that’s why I asked you before if it included it in components, look:

$config = [
'id' => 'basic',
'basePath' => dirname(__DIR__),
'extensions' => require __DIR__ . '/../vendor/yiisoft/extensions.php',
'components' => [
    'cache' => [
        'class' => 'yii\caching\FileCache',
    ],
    'mailer' => [
        'class' => 'yii\swiftmailer\Mailer',
    ],
    'log' => [
        'class' => 'yii\log\Dispatcher',
        'traceLevel' => YII_DEBUG ? 3 : 0,
        'targets' => [
            [
                'class' => 'yii\log\FileTarget',
            ],
        ],
    ],
    'db' => [
        'class' => 'yii\db\Connection',
        'dsn' => 'mysql:host=localhost;dbname=stay2',
        'username' => 'root',
        'password' => '',
        'charset' => 'utf8',
    ],
],

];

El otro punto es que se me ocurre es atreverme a darles una sugerencia como usuario del framework, deberia haber mas tutoriales como en otras herramientas, la documentacion es muy buena pero a veces necesitamos la practica y ver ejemplos con escenarios de la vida real, un sistema de stock inventario, facturacion, una red social, etc. Mira, te dejo estos enlaces que encontre en youtube y son muy buenos he aprendido mucho, pero para el caso que planteo aqui quiza estaria bueno ver cuestiones avanzadas, eso haria mucho mas popular el framework, mas si apuntan a seguir como esta planificado con Yii3:

Well it is simply a suggestion for the community, and if I were an advanced user I would contribute as I can to this, but it would be good as an official point in addition to the documentation, good videos.

I hope you can answer the first point about the container and really now it would not bother me anymore.

P.S. : I have seen a video of you on youtube, https://www.youtube.com/watch?v=WzLXWI2rD3Y&t=1745s, unfortunately it is very difficult for me jjaajjaja since we have a lot of difference in the language, but I congratulate you it is very complete . Regards!

Why not use a Form class to abstract validation with multiple models and business rules?

Hello Lucas! Thank you very much for your answer!

Look, I think what you suggest is that you establish different scenarios for different validations? that already exists in the models, but in my case what I manifest is whether it is convenient to include in the models an active logical record that does not have to do directly with the data, such as the methods I mention, an image upload, the setting of addresses , phones and mails in intermediate tables.

Although I am reading the step-by-step documentation of yii2 again, and at no time does it suggest the use of repositories or service layers, although yii2 does not restrict it, it suggests that all logic be pushed to the models, whether they are active records or not.

In short, maybe it is what I will do, if I can think of using interfaces to sign method contracts to use in different entities and then each one to do its implementation.

Cheers!

@NLeone, I think @lucaswitch suggests using form models instead of active record scenarios. That makes sense overall and proved to result in cleaner code.

@samdark, @lucaswitch

Ahhhh, I understand then. Thank you very much for the clarification, but I think my problem is not one of validations, in fact what I was looking for was to separate the logic a bit.

The validations I have done both in active record models that extend from BaseActiveRecord that in turn extends from Model, then I have done the validations, and I pass from the controller to the form view the 4 models, the client, the related model addressCustomer , phoneCustomer, and mailCustomer, which is why I use it in dynamic form and I make the whole form in one, it respects the validations in ajax, everything.

My problem was being able to reuse these methods of saving contact data for other entities, and that’s where I started to over-analyze too much.

Now I am reading this document that is super complete, Yii Guide 2.0 and a little better ordered than the web guide, . And I understand that in yii2’s philosophy it is to push all the business logic to the model, whether it is an active record or not, so I don’t know if I’m going to use a design pattern as a repository because I don’t know if it’s really necessary for my development . Maybe if you use interfaces or helper classes. So as not to get too complicated. On the other hand also for what are custom searches with complex criteria in the database, I can use the searchModels provided by the creation of CRUDs with the gii tool, and there I have quite separate what would be complex queries for data manipulation and the subsequent step to a grid through a dataProvider, isn’t it? Isn’t it something long enough?

At some other time I will enter into those dilemmas that now only lead me to delay the delivery of my work.

Thank you all!

Yes, it is hard not to over-complicate things. Takes time and practice to see where complexity is necessary and where it is better to be avoided.

Yes, I will do that, you have enough experience and I am taking these steps on yii2. What if in the future I would like to see how to apply the repository pattern in the framework because I read the theory, but a good practical example would be ideal. I was reading that yii3 comes more or less out there, I don’t know if I read wrong, but it was a publication in this forum in which you participated. So I don’t know how it should be organized in the folder structure and if it should be applied with all the active record model classes that represent tables with data, or you can simply create repositories of some classes that require it.

I think that a system made with yii can grow much more if this type of examples and practices are taught.

Greetings and thank you very much @samdark !