Skip to main content

MVC Best Practices, (Tweaking Yii Framework)

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. 


Comments

Popular posts from this blog

PHP Tutorial–Guest Book–System Definition and Requirements

In this lesson I’m going to illustrate the system definition and requirements for the Guest Book application. It is an important step in developing any application, since this step gives us an orientation about the system and its functionality, what is expected from the system to do. actually the system definition comes from keywords from customers and end users, usually try to make it clear for what they need in the application, and what they expect the system to do.  As developers it is our role to define the requirements for the system to be developed. In our example the system definition for the Guest Book is: A system that allows visitors of the site to post their comments and feedbacks about the site, with the possibility for managing comments and maintain it easily and user friendly. On the other hand, the system requirements are: a web server, since it is clear that the developed system is going to run on the Internet, so it is a web-based application not windows-bas...

تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد

مرحبا أصدقائي يسعدني أن أبدأ معكم سلسلة حلقات في تعلم تطوير تطبيقات للموبايل باستخدام الأندرويد ، وسأسعى جاهدا معكم في أن تكون هذه السلسلة من أوائل السلسلات في اللغة العربية لتعليم برمجة تطبيقات الأندرويد من البداية وحتى الاحتراف. وسأحاول قدر المستطاع الشرح بلغة عربية بسيطة ومفهومة. حيث ستكون غدا إن شاء الله الحلقة الأولى من تعلم تطوير تطبيقات الموبايل باستخدام الأندرويد. أنس الجاغوب Twitter: @anasjaghoub Facebook: anasjaghoub

AABU GTUG Opening Event

On February 17, 2011 We’ve headed to Al Al Bayt University, to run one of the biggest event established there, for opening the AABU GTUG (Google’s Technologies Users Group). The event focused on introducing the GTUGS (Google’s Technologies Users Groups) and introducing Google’s technologies to students, such as Android and App Engine, to Chrome Extensions and HTML5 demos. Attendees exceeded 250 attendee, in addition to Vice president of Al Al Bayt University Dr. Hashem Al Masaeed, Deanship of College of I.T at Al Al Bayt University Prof. Ismail Ababneh, and a quite number of professors and teachers at the college of I.T at AABU, in addition to Yarmuk fm and Al Ro’aya  tv. AABU GTUG as a Google’s Technologies Users Group interested in Google’s technologies and tries as possible to increase students awareness about Google’s technologies, and introduce it to them. Their was a lot of activities during the event, beginning with the key note from me Anas Jaghoub, that included introducin...