Php mvc implementation (4) our index. php file looks like this:
The following is a code snippet: Require_once ('Lib/DataAccess. php ');
Require_once ('Lib/ProductModel. php ');
Require_once ('Lib/ProductView. php ');
Require_once ('Lib/ProductController. php ');
$ Dao = & new DataAccess ('localhost', 'user', 'pass', 'dbname ');
$ ProductModel = & new ProductModel ($ dao );
$ ProductController = & new ProductController ($ productModel, $ _ GET );
Echo $ productController-> display ();
?> |
Beautiful and simple.
We have some techniques for using controllers. you can do this in PHP:
$ This-> {$ _ GET ['method']} ($ _ GET ['param']);
One suggestion is that you 'd better define the namespace format (namespace) of the program URL, so that it will be more standard:
The following is a code snippet:
"Index. php? Class = ProductView & method = productItem & id = 4"
|
With this, we can process our controller as follows:
The following is a code snippet:
$ View = new $ _ GET ['class'];
$ View-> {$ _ GET ['method'] ($ _ GET ['id']);
|
Sometimes it is very difficult to establish a controller, for example, when you weigh between development speed and adaptability. A good place to be inspired is Apache group's Java Struts. its controller is completely defined by the XML document.