twitterfacebookgoogle plusrss feed

Pages

Friday, March 28, 2014

How to create file and directory structure for MVC


You will find dozens of free and opensource MVC fameworks (Zend, CodeIgniter, CakePHP are some of them) around you, loaded with hundred of features so on this point making your own MVC is not a good idea. But for practising purposes I will tell you how MVC works and how to build directory and file structure for your very own MVC.

In old fashioned web page/application we handle user input, manipulates or use databases and then redirect user to beautifully organized results suitable to user input. All this activity occurs in single page, its okay if we are working on small application where much business logic isn't repeated. But in enterprise application this approach possibly make duplications in the code.

So what should we do if we are going to build enterprise level application.


MVC (Model-View-Controller)

It is a software pattern or architecture divided and interconnected into three parts, overridden old fashioned web page/application. Controller takes the user input, manipulates and convert input to command and send the formatted user commands to Model. Model is collection of logics and a place where we run database operations and send back required information to Controller, Controller then injects all user requested information in to the View. View might be collection of standard HTML commands, diagrams, charts, Javascript, CSS and any possible client side script. Take a look at the following flow chart it will help you to better understand MVC architecture.





Note: Kindly pass comments if you found any logical problem in flow chart, your comments will worth me a lot.

Break apart

Model: As per Controller request informs associated Controller and View about changes, so View change its representation and Controller update commands.

View: Controls the representation according to Controller commands.

Controller: Manipulate user input and send commands to Model to update model's state. Compile and inject the final out put to the View.


MVC files and folders structure

You need 4 folder and 2 files on the root

1) model (dir)
2) view (dir)
3) controller (dir)
4) engine (dir)

1) index.php



2) .htaccess

"model" directory contains all Models (files) or project.

"view" directory contains the files for the project used as front-end. User will serve and see these files.

"controller" directory contains Controllers (files) for the project

MVC has three basic files, main Controller, main Model and main View file known as engine for your MVC framework. These three files extends by all other Controllers, Models and Views. Besides these files you may also need helpers and libraries to be imported later in the project also placed in "engine" directory.

"index.php" file is very crucial, this is main switch to start your framework. Be careful while approaching professional level. Things started at this point will be available to all our project.

".htaccess" optional file. Just to beautify your URL to make it search engine friendly. Without this file your framework will function properly but I recommend to use it to make your project search engine friendly.

0 comments:

Post a Comment

comment or ask