Today, I'm going to talk about MVC, what is it, why we should use it in web development, then we'll dig a bit to Yii framework, to see how does it work, and talk about best practices of MVC's and Yii framework as well. On the other hand will talk about tweaking Yii framework to speed up a website performance.
What is MVC?
- MVC is not only a 3 terms combined together.
- MVC is a design pattern, a software architecture, a standard used by developers to organize high scale projects.
- The main idea behind using MVC: code re-usability, separation of concerns, modularity, scalability, and maintainability.
- MVC is a Model, View, Controller. where the model represents the data layer, the view represents the presentation layer, and the controller responsible for handling requests, managing models/views.
What is Yii?
- Yii is a high performance, component based MVC framework, for developing web applications using PHP.
- Yii, one of the fastest/efficient/lightweight/feature-rich frameworks.
- Yii can be used for developing any type of web applications, because it is lightweight and supports caching.
- Yii is purely implemented using OOP.
- http://www.yiiframework.com/
Why Yii?
- Implements the MVC.
- Intensively follows OOA/OOP standards, such as (Classes, Objects, Abstracts, Interfaces, Name spaces, Inheritance, Polymorphism, Static functions, Over-riding, Singletons, Factories, Black boxes…etc).
- Has a useful documentation, and all its classes/functions fully documented with code samples.
- Supported widely by community, and has a lot of extensions, widgets, that helps developers doing prototypes easily and in minimum time.
- Open Source, Free to use,share and update.
How Does Yii Works?
Tweaking Yii
Performance of web applications is affected by many factors, like db access, I/O operations, network bandwidth, 3rd party services...etc
On high traffic applications, many tweaks can be done that enhance the performance of the application.
Talking about tweaks/tricks in details will take us long time, so today we are going to talk in high level about concept and techniques. Hopefully in next articles we discuss each tweak in more details, and provide show cases before/after the tweak.
1. Enabling APC Extension:
PHP APC extension caches and optimizes PHP intermediate code, and avoids the time spent in parsing PHP scripts for every incoming request.
2. Disabling Debug Mode:
Many Yii components have too much logging statements that run in debug mode, so disabling debug mode will stop logging non-necessary code snippets.
Note: Debug mode is disabled by having the YII_DEBUG set to false.
3. Using yiilite.php:
By default Yii bootstrap file is yii.php, which is responsible for bootstrapping Yii, and including common libraries and scripts. On the other hand, on each release Yii provides a file named yiilite.php which is a clean/comments free version of the yii.php file, plus with a reduced number of script includes, since common libraries already merged to one file.
4. Caching:
Frequently accessed data should be cached, such as db schema, complex view, models …etc
5. Database Optimization:
It's not a secret that Active Records degrades performance on both db level and parsing level, due to the fact that each active record instance represents a connection to the db, and can run heavily queries to db, and might append unneeded conditions to the query specially on count queries.
Active Records is good for modeling data in an OOP fashion, but on the other hand degrades the performance of application.
Hence it is preferred to have heavily queries, queries that deals with enormous data, to be run using low-level database APIs instead of using active records.
Queries should avoid unneeded joins, and try to lazy loading related models instead of eager loading.
SELECT queries should have LIMIT statement to avoid exhausting the memory allocated to PHP.
Tables with enormous data should be indexed.
It is recommended to use database views for complex queries instead of issuing the queries from PHP and passing it to DBMS to parse them.
6. Minimizing Script Files:
External Javascript/CSS files should be minimized and combined, to avoid too many requests from each client, and hence reduce the network transmission time.
7. Symlinking assets:
Asset manager after doing include/minimize/combine process copies generated files from their original path to assets directory, this process can be tweaked by stopping the copy process and replace it with a symbolic link to the generated file.
Conclusion
- Yii is one of the best MVC frameworks that can be used to develop web applications.
- Yii is lightweight, efficient, and feature-rich framework.
- Yii intensively follows OOP standards.
- Too many tricks/tweaks can be followed that increases application performance.
- Controllers should be thin, models should be fat, views should avoid too much logic, and/or dealing directly with model logic.
- Yii is a high performance, component based MVC framework, for developing web applications using PHP.
Comments
Post a Comment