
This article will discuss application development on Yii2. Namely, as at the very beginning of your journey without a certain experience, it is easy to succumb to temptations and turn onto the road that leads directly to HELL. Further, the word HELL assumes a situation in which you understand that it is becoming more and more difficult to accompany your code.
The proposed information may be useful for beginners and average developers, professionals may find it a complaint, which may well be true.
')
A little backgroundThree years ago, I became acquainted with a great tool called Yii2 and began to work closely with it. First, to learn new php5.3 and php.5.4 fitsch and in the future to implement all of their projects. Then I also had very big problems with architecture, although I have to admit that now they also exist, but not on such a scale. Therefore, when I ran into a lot of mistakes, I did not attach any importance to them, and when the project grew, it was already too late.
So let's get started.
Yii is a highly efficient component-based PHP framework for developing large-scale web applications.- Is this the statement that the developers write on the Russian site Yii2 true?
- Is it really possible to write a project of any complexity on Yii2?
The answer to these questions is definitely yes. Yes, but with a small such condition, if you or the person from your team is an excellent application architect or a very experienced programmer and controls each developer.
Yii2 as a whole is a good tool! You can find many advantages in it and, of course, make sure yourself developing it on Yii2. However, for a novice developer with no specific experience, development on Yii2 can be sideways in terms of the growth prospects of the project. At the very beginning everything is great, everything works, good documentation, a strong community, it is easy to find the answer to your question.
However, if you have a good project that is constantly evolving and more and more functionality appears, one day you will wake up and realize that you are in HELL. And getting out of it will be very, very difficult.
Application Development Process on Yii2Installing Yii2 is simple enough. With the advent of composer, installing almost any tool is simple. The only thing that spoils it is the composer-asset-plugin, many have problems with it. Someone forgot to install, someone updated and did not reset the cache, in general there are a lot of problems and issues on this topic. Although I will not record it in the negative, just say that installing Yii2 is quite simple.
Select an application templateYii2 is kind enough, so it offers several templates for the architecture at once. Classic is the choice between advanced and basic. Probably every newcomer, seeing Yii2 for the first time, thought about the question which template to choose?
I did several medium projects on different templates and always ran across different problems in each of them.
- basic
I do not know about you, but I had discomfort when the project was constantly growing. It was necessary to think how beautifully to make the admin panel, if it is organized by a module, then there is a big nesting, a feeling of inconvenience, and even if suddenly for some reason you would need to add another application (in terms of growth as advanced) the discomfort increased.
- advanced
How would solve the discomfort with the applications in the basic, but there is a new discomfort. In our project, a ton of configs appears for each application + common and all models begin to divide between applications and inherit each other. In some cases, it was necessary to overlap the reports in order to pull classes of the models of the current application, but not the general one.
In general, the more the project grows, the greater the discomfort that comes from basic, that from advanced templates. This problem is solved, you can make your template, however, to come to such an idea you need to have a good experience in order to understand all the minuses and advantages. Let's say an ordinary developer will not think about it at the very beginning of development.
From about this moment we have already looked (not stepped, but only looked) at the road that leads to AD.
Yii2 - modern mvc frameworkEven if we assume that everyone who develops on Yii2 (although this is far from the case) follow the mvc paradigm, write thin controllers, fat models and do not write logic in views, while working with Yii2 you are all the same in ADU.
The fact is that the developers of Yii2 themselves interpreted MVC too literally when developing the tool. As a result, the model is obtained by such a divine class that does everything. The model is usually inherited from ActiveRecord in the best traditions, works with forms, validation, contains logic, relays, another logic, more scripts, and 10 more logics for each script and another logic for logic of logics.
When the project grows and you have a model that, say, consists of 5 scenarios, contains a lot of logic and 80% of your application depends on it even in the best traditions. And then the task appears to change or add something, and you understand that you have not just one such model, but as many as 10 and they all depend on each other and in order to add something you need to refactor the whole thing. Look around and see the road with a sign "Welcome to Hell."
Perhaps the most basic problem of Yii2 is precisely that almost all inherit models from ActiveRecord and implement all the logic in it. There are still variants, when the logic is separated by controllers and views.Some examples from life (or welcome to AD):- In one project, I witnessed that the developers adhered to the idea of ​​MVC, made thin controllers and fat models. As a result, it was possible to observe several models from 10,000 to 15,000 lines of code that were associated almost with the greater half of the entire project. The moment came naturally when everyone was baptized and just afraid to touch them, knowing that if the whole project could break something. This is not critical on a small project, but if the project is gaining popularity and growing, wait for trouble.
- If we inherit all the models from ActiveRecord, then at the exit, in perspective, we also get a bunch of problems. Perhaps there will be a situation when we inherit the frontend and backend models from common models, which in turn is from ActiveRecord. But what if, suddenly, by common logic, we need different logic in frontend and backend applications? Most likely overlapping scenarios, events such as afterSave, beforeSave, and many other interesting things will begin. With whom I came across almost everywhere.
- ActiveForm, here is just facepalm. This is not a very flexible thing, like 90% of Yii2 widgets, they are designed for quick development, let's say prototypes or maximum admin area. But using these widgets in a large and complex project is not a good idea. Practically it is not realistic to realize the perverted tasks of clients. Even as a complaint, this is what the developers of Yii2 decently mixed php code with js, this can be seen in the validators. And finally, this is what all polls depend on jQuery. Not to say that this is bad, but there is still such a wonderful framework as vanilla (if you understand what I mean;)).
- Extensions. As for the Yii2 extensions (of course, from third-party developers), 99% of these extensions solve only primitive standard tasks. If a serious serious problem arises, then in 99% of cases you will have to write everything yourself.
- And there are a lot of such examples.
Of course, all these moments fall directly on the developers. However, when I started, I couldn’t even imagine that in a year I would have more than 80 models with twisted logic and a bunch, just a huge bunch of conditions, so that at least “this” would work. Refactroing turns into AD, because dependency recursion does its job. If you somehow refactor one model, this is followed by refactoring almost half of the project.
Perhaps the most important cons of Yii2, which at one time spoiled my life- Too good and a lot of bad allows the developer. In some moments, even provokes a dirty trick.
- Most connected components.
- Initially, Yii2 does not introduce such a thing as layered architecture. As a result, divine models appear at best, but at worst everything is scattered like any other.
- DI. Compared to symfony2 (or 3) in Yii2, DI is a toy for an infant.
- ActiveForm. Namely JS api. Although on the validators a little bit too possible.
- Third-party extensions. In 99% only primitive standard problems are solved.
Naturally, professional programmers easily bypass all these drawbacks, since they understand by experience what to expect. Unfortunately, I once made a lot of architecture errors and in the future it turned out to be a very big side.
Therefore, try not to repeat such mistakes and not start your training on Yii2.
Some tips for those who work with Yii2- Write at least unit tests. This is a great help, especially when it is a big project on Yii2, where everything is connected.
- Prevent the appearance of divine fat models
- Do not inherit everything from ActiveRecord
- Do not write all the logic in the models, divide the models into services (into separate classes).
- Try to avoid connectedness as much as possible.
- Do not write logic in views and controllers.
- In the evenings, take the time to symfony 2 (or 3) to broaden your horizons.
Even as a supplement, I want to answer some popular questions:What framework to choose a newbie?A professional developer can choose absolutely any tool for project development. As for the newbie, I would recommend taking symfony 3. Just take a word. May it be hard, may there be a desire to leave, may there be complaints. You need to endure and just sit on Symfony 3. And so on until you figure out the architecture and get a good knowledge of "how it works." After that you can avoid many mistakes in Yii2.
Is Yii2 suitable for large projects?Yes. Yii2, like any other modern tool suitable for any project, but a prerequisite should be that you or the representatives of your team are good engineers. Otherwise, when the project grows you wake up in hell.
Symfony2 vs Yii2, which is cooler?Previously, I always defended Yii2 with both hands and sinned that Symfony2 is slow, fat, there are a lot of unnecessary things in it and so on. I appeal to all symphonists: Sorry, guys, I have never been so wrong.
As for the answer to this question, as Alexander Makarov said at one of the conferences: "Yii2, this is another niche."
Comparing Symfony2 (or 3) with Yii2, it's probably like comparing a bike with a car.
In theory, by bike you can also drive 500 km. However, be prepared to stock up on glue and patches.And yet, many would now be happy to relax and ride a bike.
This article in no way argues that Yii2 is a bad tool. Rather, the article warns that when developing on Yii2, you need to be extremely careful and think through every step, as the framework just beckons you to go wrong .
Further, if the topic is interesting to someone, then in the next article I will try to talk about how you can take the true path and work more or less beautifully with models in the Yii2 style.