twitterfacebookgoogle plusrss feed

Pages

Showing posts with label MVC. Show all posts
Showing posts with label MVC. Show all posts

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.

Thursday, March 20, 2014

Get reference from URL for MVC framework


Look at following URL

http://www.example.com/records/dvds/10

While creating MVC framework you have to follow the URL for reference. In above URL "records" is class or controller name. Controller is used to accept user input, after collecting data it prepare commands for Model and View. Reference to above-mentioned URL, "dvds" is a method of Controller "records" and "10" is parameter of method "dvds".

Note: URL is rewritten using .htaccess (check this post)

Now the question is how we will split the URL to get controller, its method and parameter for method. 

First get the URL by 

$route = $_GET["route"]; // or what ever you passed in .htaccess

Split the $route with "/"

list($class, $method, $param) = explode('/', $route); 

Now $routes is array holds controller name and its method and parameter if passed. 

if (is_file('controller/' . $class . '.php')) {
    $file = 'controller/' . $class . '.php'; // get the file of the Controller
}

Now you have, Controller, Controller file (where class or Controller written), method and parameter. Remember all three parts of URL (Controller, Method and Parameter) will not be passed every time, so be careful when approaching higher level.

Tuesday, March 18, 2014

.htaccess for MVC


In MVC routes are normally sent through query string formatted using .htaccess. You are passing a query string to index.php i.e. domain.com/index.php?route=records/dvds/10 transform into domain.com/records/dvds/10. What you have to do is create a new file .htaccess using any supported editor (netbeans will be best, remember notepad of windows do not support a file with no file name) and put following lines into the file, save and place this file in root of the folder where you want to create operational MVC.


RewriteEngine On

RewriteCond %{REQUEST_FILENAME}% !-d
RewriteCond %{REQUEST_FILENAME}% !-f
RewriteCond %{REQUEST_FILENAME}% !-l

RewriteRule ^(.*)$ index.php?route=$1


It will successfully tranforme junky query string into nice looking url. The first part of url is considered a controller or class, the second part after front slash "/" is known to be method and the last one is parameters for the method. If "route" in query string is records/dvds/10 (looks like domain.com/records/dvds/10) you have to explode this string on "/" and it will returns an array contains three pockets, 0=>records (controller or class), 1=>dvds (method of records), 2=>10 (parameter of records).