Building Web Solutions: Pros and Cons of Yii2 Framework
It’s important for non-technical startup owners and their assistants and researchers to understand what they’re going to face in the world of programming. This read is the first of our series Tech for Non-Technical Founders. It contains:
- small bits of information about PHP, a programming language for web;
- a bit about frameworks and what they do and why engineers need them
- about the pros and cons of Yii2, our favourite PHP-framework, from the business points
To the reader: we’re using Yii and Yii2 interchangeably to make reading easier, so keep in mind that we’re talking about Yii2 only, unless otherwise noted.
Understanding PHP: a simple language for web
People often are operating under the stereotype that PHP is outdated and/or terribly confusing and inconsistent, so let’s deal with it first.
79% of websites whose server-side (back-end) language is known are written in PHP. This language was developed specifically to build interactive web resources and to do it well.
Wordpress, the most popular content management system on our planet, is written in PHP, and it holds 60% of CMS market share. Yahoo, Tumblr, Wikipedia, and even Facebook are developed using PHP. So no, PHP is not outdated.
Bjarne Stroustrup, creator of C++ — it’s an another programming language that’s now used for games and hardware mostly, — once said: “There are only two kinds of languages: the ones people complain about and the one nobody uses.” This phrase can be used as an answer to PHP inconsistency argument. In programming languages, there is always a room for improvement: make them faster, simpler, more secure. Most of the time, inconsistencies in the development are due to developers, not due to a language.
So, bottom line: PHP is perfect for any web-tasks. Simple content management systems for blogging, portfolio, e-commerce platform or complex enterprise solutions, embarking on connectivity and interoperability, like solutions for business process automation or hospital management — all that can be written in PHP.
Understanding frameworks: Why developers need them
So, you need a web-app or website. You’ve chosen PHP. Next thing that is good in PHP — it’s a mature community, a lot of open-source frameworks and ready-to-use features. What is it and why is it good?
> What is a framework
By Wiki’s definition, a framework is an abstraction in which software providing generic functionality can be selectively changed by additional user-written code. Basically, it’s a semi-app that is ready to use, a skeleton that provides programmers with reusable software elements (classes, objects, etc.) and contains a place to play — a place to code and customize mentioned elements.
> How frameworks work and why are they good for coding (and for business)
Let’s say you write a program that asks a user the Ultimate Question of Life, The Universe, and Everything. You’ll have to put down commands like print Enter your ultimate answer and then read answer, and a program would pick up a response to it. So you’re in control of what’s going on in your code.
In a framework, this process is tied to user behaviour and the code doesn’t know what command sounds like. So, our example in a framework will sound like this:
when the user types in the field A, store it in the Ultimate Question of Life, The Universe, and Everything
if the user types “42”, call StoreInDatabase
Raw code vs framework code
The second example is an example of a phenomenon that is called inversion of control. It’s a common characteristic of frameworks: instead of handling dependency by yourself, like in the first example, you let a third party (a framework) — to handle it. This term was considered as too generic for programmers, so Martin Fowler and his colleagues came up with a term dependency injection.
>> Business benefits of frameworks
- In code, fewer dependencies mean less probability of cascade fail. Fewer fails = fewer fixes = saved time & improved code quality.
- Reusable artifacts like classes, objects, and components make programming easier and faster. Again, developers don’t waste time on reinventing the wheel and have more time to approach challenges an app solves more creatively and thoroughly.
- Features that help integrate the framework with open-source libraries also accelerate building.
- Raw code requires developers to develop security measures. Good frameworks have built-in features conveying these measures so there are fewer risks of typical security issues.
- Programming in a single framework is widely adopted by businesses, especially by enterprises: in the framework, you can establish coding standards that will be used by everyone. It’s like editing policy: makes all solutions, developed by businesses, with a consistent structure. Thus, solutions built through frameworks are easier to scale.
- Additional, frameworks are often fun to use and you can actually contribute to the next version. If you a programmer.
Now, when we learn why frameworks are a good thing in general, let’s move to the Yii and why it’s (in our opinion) is a perfect choice for web apps everywhere.
Yes, It Is by Qiang Xue
Yii (an acronym for Yes, It Is) came alive on the first day of the 2008 year. In case you’re wondering (as the writer was) how it’s pronounced, it’ll be Yee or Jii. It’s also a Chinese name that translates as simple and evolutionary.
A year was terrible, but Yii was good: a light PHP framework with clearly written documentation, that made web-development significantly faster, than other frameworks, existing back then. In October 2014, the first stable version of Yii 2.0 was released.
So, what Yii2 can do and why it’s worth considering for building on the web?
As the other PHP frameworks, Laravel and Symphony, Yii is an open source framework that supports both front-end and back-end programming. It has a lot of explanatory documentation and it’s supported by a large community (its community, though, is the smallest out of three). Also, all three frameworks support Object-Relational Mapping (ORM).
ORM is a programming technique that converts data between incompatible type systems in object-oriented languages (as, in our case, PHP).
Yii’s developers, in a way, created a wheel: they’ve written their own ORM, Yii Active Record: it eliminates repetitive tasks that cover CRUD (create, read, update, and delete) actions in database programming — in other words, reduces hand coding. That feature, according to the guidelines, was inspired by Ruby on Rails.
Pros of Yii
Low entry barrier: Easy-to-learn
Our developers describe Yii as the most simple PHP framework. Documentation is written in a very easy, understandable way, open-source community is there to answer a learners’ questions, and, generally, to start coding in Yii developers require a few days.
>> Business point. Your team can adopt the framework very quickly.
Lazy loading technique: Fast coding
So, you probably know what is a database. To retrieve data from a database, you have to ask a database for it. The request for data is called a query. There are query languages — languages, in which queries are written, — and one of them is called SQL.
The thing about queries in programming is to write a really fast application, you have to use queries in a way that won’t overload memory of an app (cache). You don’t want to use too much data records. You want to use some specific amount of them.
So, in Yii, if you ask for comments about “apple” through an eager-loading approach, all comments, associated with an “apple” in your data table will be put in the memory of an app, ready-to-use. And when you use these comments, all of them will be retrieved from memory, not from the database. So, there will be one big (and heavy) query.
However, using the lazy loading approach, you can get a specific record about an “apple” straight from the database and only if you refer to it. Thus, no overloaded memory.
>> Business point: When memory isn’t overloaded and a few resources are used, the performance of an app is very high. Usually, web app doesn’t require a great capacity to perform, but if we’re talking about real-time solutions, like a website with an integrated communication platform, a chatbot, or about social media, that’s another story. Plus, these features allow to generate code faster and, therefore, saves product development time.
Gii, a CRUD generator: reduced hand coding
Gii is an extension for Yii (yeah, a lot of nicely-pronounced names) that… automatically generates code. Yep, that’s right. Repetitive work must die. It’s a tool that automatically provides you with often used pieces of code (snippets) or even with complete CRUD controllers. Through Gii, it’s also possible to create customized templates for models, controllers, forms, modules, extensions — all in semi-automatic fashion and through simple commands.
Automatically generated code means the future is here. Maybe soon we will get flying cars.
>> Business point: Programming is, like, super-fast. Really. Also, we suspect that automation increases job satisfaction for developers, because… you know… code practically writes itself. Plus, all these semi-automatically created and customized components are reusable, so it’ll be very easy to scale your product.
Validation, Authentication, Authorization: secure app
Yii provides a lot of features to secure application. To filter a user’s input, Yii utilizes tons of different validation features that are performed through rules and scenarios that are tied to models or conditions and that dictate how validation should proceed. Developers can also create custom validation features via Gii. Authentication and authorization are supported; in Yii, these are ready-to-use classes that can, f.i. allow logging in and viewing hidden articles, but not editing them. Automated audits protect web app’s cookies and eliminate vulnerabilities from app’s code.
>> Business point: Security of an application. There is a tone of features to protect an app, really, but beware: despite the fact that hand coding is minimized, security still highly depends on your developers’ abilities.
Easy testing: Less Wrong
To test an app written in Yii, you’ll need one extension: Codeception, a highly flexible tool for automated testing. Testing is customizable: you can set up tests that, for instance, won’t use a production database or won’t send you real emails.
Codeception is perfect for test-driven development (TDD) and behavior-driven development (BDD). These approaches to implementing software describe parts of code or whole features as multiple scenarios or tests before writing code. They’re good when you’re unsure what to do next, or the project gets more sophisticated, or the cost of failure is too high. Surely, there are other solutions to these issues. Our point is that even with a TDD approach when you test things in Yii before you write Actual Code in Yii, the pace of product development doesn’t really slow down. And the code quality gets better.
>> Business point. Quick, but mindful testing is your way to reduce errors and prevent unpleasant surprises, and improve code quality. There’s no question about the importance of happy users.
Model-View-Controller (MVC) Architecture: Less Wrong 
MVC is a programming design pattern, that divides an application into three components (obviously):
- model — represents application data;
- view — conveys representation of data in design; simply, it’s the user interface;
- controller — represents the business logic of your application.
A nice thing about MVC architecture is that each of these components can be edited separately, so it’s easier to build, test, and maintain an app.
Yii also includes front-controller called “Application”: it “sees” what user is doing, encapsulates the context of the user request, process it, and then sends it to a fitting controller. This feature makes applications more responsive, as the context of user behaviour is pre-analyzed, so the request gets into a controller that is customized to handle it.
>> Business point. Easier, faster building and testing of an app; higher code quality due to an opportunity to edit each component independently; higher performance of an app.
Integration with RESTful API : Quick development of mobile and single-page apps
If you want to connect your website to the cloud or to a mobile app or to a single-page app (SPA), you have to use the framework with integrated RESTFul API. API it’s an application product interface that ties different software products, connecting their data and functionality — without interactions with source code. REST stands for Representational State Transfer, a software architecture style that describes constraints (conditions) for creating interoperable web-services.
For instance, in healthcare, you’ll need API to connect your EMR to a mobile app for hospital management to ensure a) EMR gets new data from new-coming patients, b) nurses have real-time access to information about patients’ condition.
Yii’s features allow quick prototyping on the basis of common APIs, and it’s very easy to build a set of to connect, for instance, with Angular or React Native and build a mobile app or to AMP to make site’s pages mobile-friendly in few clicks. Routes for data streams are created automatically and are easily customized.
>> Business point. Connectivity and scalability. A way to quickly set-up a mobile app, a mobile version of your product or a landing page for promotion of a new service.
Cons of Yii
Slow development of new versions: Waiting
Updates of Yii are rather rare because its community is smaller than communities in Laravel and Symphony. Plus, its creators have a little time to update bug fixes, proposed on GitHub. On the good side, your team can participate and help Yii people handle bugs and contribute to the development of the tool they’re using.
>> Business point. If some extension, method, class, etc. don’t work right, you’ll have to wait until the bugs are fixed and the minor release is updated.
Requires a lot of discipline: Experienced developers are needed
Despite the fact the entry barrier for Yii is rather low for experienced developers, juniors can find themselves a bit confused with a framework. Yii is a result-oriented framework that is aimed at Getting Things Done Quickly. To be able to write good code in it, one needs to a) truly understand what Don’t Repeat Yourself rule means, b) be careful and mindful of what and why is going on in the code, c) think ahead of now. Juniors are generally don’t possess all these qualities from the start, so it would be challenging for them to get the framework quickly.
>> Business point. It’s better to organize mentoring and run some learning and development initiatives if you’re planning to hire developers with little experience. It’ll be fair to note that juniors all by themselves in scary and important engineering projects aren’t recommended to work without mentors or middles/seniors in general, in any programming language and within any framework. Yes, the cost of hire is low, but the headache and lost time honestly don’t worth it.
Too many libraries & dependencies: Work can slow down
There are a lot of libraries, automatically integrated into Yii, but only 20% of them are used actively. So, by default, Yii uses rather many unnecessary resources.
>> Business point. The pace of development can drop. However, there’s a possibility to disconnect unnecessary libraries, if there’s a noticeable time loss — although, in Yii, it’s rather a rare thing.
Conclusion: we love Yii and we’re not sorry
In 2016, the framework’s creator Qiang Xue published an article called Philosophies that Shaped Successful Frameworks. He said, “Building a successful framework is all about striking the right balance between power and simplicity”.
We think that balance was discovered in Yii: developers say this framework is an opportunity to finally focus on design and logic of an application and almost fully rely on Yii in the coding matters.
At the same time, we worked with Yii for the last few years and, collaborating with business owners — especially with executives who are just at the beginning of their ways — we figured that fast and highly performative programming with simple logic and light syntaxis is the best way to set up a young solution. And Yii provided us with such an environment.
We want this article to become more comprehensive in terms of benefits and inconveniences in Yii, so if you want to add pros or con to the article (with a business explanation of them), write our marketing team: firstname.lastname@example.org
Your submission is received and we will contact you soon