twitterfacebookgoogle plusrss feed

Pages

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.

0 comments:

Post a Comment

comment or ask