Page 1 of 1

understanding MVC

Posted: Fri Nov 21, 2014 11:03 am
by gautamz07
given the below snippet : ()


index.php in main directory

Code: Select all

<?php 

	$url = $_GET['url'];

	echo $url;


	require 'controllers/' . $url . '.php';

	$controller = new $url;
?>

index.php in controller

Code: Select all

<?php


	class Index
	{
		
		function __construct()
		{
			echo "We are in Index";
		}
	}

?>

in my browser i see the following (i.e if i type ... mypath/index):

We are in Index.

now i don't quit e understand this line :

$controller = new $url;

what is the above line doing ?

is it instantiating $controller with a new instant of $url

or is it calling the class directly to which $uri corresponds to ???

Re: understanding MVC

Posted: Fri Nov 21, 2014 11:12 am
by Celauran
It's assuming $url is going to be a valid controller, requiring the file from the controllers directory -- which means the whole thing will come crashing down if it doesn't exist -- and then instantiating the class.

Re: understanding MVC

Posted: Sun Nov 23, 2014 1:58 pm
by Christopher
You index.php is specifically a Front Controller which routes requests. Your controllers/index.php is an Action Controller which is typically what is meant by the "C" in MVC. The Controller's job is inspect the Request and then connect the Model and the View so that they can generate the Response.

The code above is using a scheme referred to as Convention over Configuration. The convention used here is a common one for MVC: the parameter value, the file name and the class name are all the same or derivable.

Re: understanding MVC

Posted: Tue Nov 25, 2014 6:49 am
by gautamz07
Thanks Guys . :)