📜 ⬆️ ⬇️

Developing applications on Yii2 without experience - a direct path to AD



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 background
Three 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 Yii2
Installing 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 template
Yii2 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.


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 framework
Even 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):


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


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


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.

Source: https://habr.com/ru/post/273615/


All Articles