Thursday, November 19, 2009

How to write own MVC PHP Framework


What is MVC?
From Wikipedia-
Model–View–Controller (MVC) is an architectural pattern used in software engineering. Successful use of the pattern isolates business logic from user interface considerations, resulting in an application where it is easier to modify either the visual appearance of the application or the underlying business rules without affecting the other.
In MVC, the model represents the information (the data) of the application; the view corresponds to elements of the user interface such as text, check box items, and so forth; and the controller manages the communication of data and the business rules used to manipulate the data to and from the model.
In simpler words-
1. Model handles all our database logic. Using the model we connect to our database and provide an abstraction layer.
2. Controller represents all our business logic i.e. all our ifs and else.
3. View represents our presentation logic i.e our HTML/XML/JSON cod

MVC Structure Work Flow


























Existing PHP Framework

PHP Popular Frameworks

    1. Zend
    2. Symfony
    3. CodeIgniter
    4.  CakePH
    PHP Application Frameworks
          1. Joomla
          2. Durpal
          3. Wordpress
          4. OSCommerce
          5. Magento
    To write the own framework, first need to understand the structure of any one the above frameworks ( zend, symfony, codeigniter or cakePHP).


    Mission

    Our mission is to build a PHP framework with MVC that is
        1. Simple
        2. Reusable
        3. Fast
        4. Extensible in future
        5. light-weight

    Core Components


    For the MVC framework structure we need the following components

    1. Controller
    2. Model
    3. View & Layout
    4. Access Control


    Directory Structure



    The framework should follow the standard directory structure to organize or hold the files.

    • /Framework or root folder
      • /application
        • /controller
        • /model
        • /view
          • /layout
      • /lib
      • /config
      • /public_html
        • /css
        • /images
        • /scripts

    The purpose of the each directory

    application - application specific code
    config - database/server configuration
    lib – library of framework code
    public_html - application specific js/css/images/scripts





    Coding Conventions 
    1. mySQL tables will always be lowercase and plural e.g. items, cars
    2. Models will always be singular and first letter capital e.g. Item, Car
    3. Controllers will alway be singular and first letter capital e.g. Item, Car
    4. Views will have plural name followed by action name as the file. e.g. items/view.php, cars/buy.php