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.

One thought on “MVC Best Practices, (Tweaking Yii Framework)

  1. http://www.apptrainers.com/

    —————————————————————————————————–

    أب ترينرز .. أفضل من يقدم تطبيقات الأندرويد و يعلمها

    تصدر أندرويد لجميع نظم تشغيل
    الهواتف و الكمبيوترات الكفية لم يكن من قبيل الصدفة ، بل لمزايا لا توجد إلا عبره ، و من أهم تلك المزايا هي جوجل بلاي ، الذي يزوره يوميا ملايين المستخدمين ، حتى أن عدد مرات تحميل تطبيقات أندرويد عبره قد تجاوزت المليار مرة تحميل ، ما جعل المبرمجين حول العالم يتوجهون
    لتعلم كيف يمكن عمل تطبيقات عليه ؛ لتحقيق أرباح طائلة ، و
    في موقعنا ” اب ترينرز ” استطعنا عبر فريق عمل متكامل و متخصص أن
    نحقق نجاحات في هذا المجال لم يسبقنا إليها أحد من المواقع العربية ، فصممنا عدد من التطبيقات المجانية و المدفوعة ، و دربنا مئات من
    المبرمجين على كيفية بناء تطبيقات أندرويد وايفون ، و بالفعل استطاعوا اختراق سوق العمل ، أصبح لهم تطبيقاتهم الخاصة التي تجذب
    المئات من المشترين .

    من أبرز التطبيقات التي لاقت إعجاب المستخدمين
    برنامج “الحافظ الصغير” الذي يمكن عبره أن يتعلم الطفل
    كتاب الله بأسلوب سلسل و شيق للغاية ، و قد عرضناه مجانا لمستخدمي أندرويد وايفون، و تم تحميله عدد كبير من المرات
    مع تغذية عكسية ايجابية من الكثير من
    المستخدمين .

    تعليم الأطفال بأسلوب شيق كان أحد
    أهدفنا النبيلة ، و قد حققنا ذلك عبر تطبيقات تعليمية متعددة ،
    و من أهمهم تطبيقين أحدهم بالعربية و
    يسمى ” دفتري ” ، و الثاني ” كول بلاي سكول ”
    يمكن للطفل عبرهما أن يتعلم عدد من المهارات الأساسية بشكل مسلي ، مثل قراءة الحروف ، الأرقام ، مع لوحة مبتكرة للتلوين، و ذلك عبر الصوت
    و الصورة ، و الحركة لضمان أكبر قدر من تفاعل الطفل
    مع البرنامج .

    النجاحات التي حققناها عبر
    تطبيقاتنا كانت سببا في أننا أصبحنا محل ثقة الكثير من المتدربين الذين يودون بداية طريقهم في
    تنفيذ و تصميم تطبيقات الأندرويد
    ، و الأيفون ، و قد نلنا شرف تدريب الآلاف من الشباب ليقتحموا
    مجال تطبيقات الأندرويد و كذلك الأيفون هم يملكون كافة المهارات اللازمة لذلك .

    نقدم في ” اب ترينرز ” مجموعة من
    الدورات المتخصصة ، و كذلك التدريب على مهارات
    تصميم التطبيقات الناجحة
    و المبتكرة على أندرويد ، وايفون
    ، عبر عدد من المختصين المحترفين ، الذين يقدمون للمتدرب خلاصة خبرتهم الطويلة ، و معلومات قيمة حول كل خطوة في
    تنفيذ التطبيقات ، ما يجعل المتدرب يحصل على مهارة ، و إمكانية
    تنفيذ أي تطبيق يفكر فيه.

    ——————————————————————————————–

    تطبيقات أندرويد

    بناء تطبيقات

    تطبيقات تعليمية

    الأيفون

Leave a comment