PHP's MVC implementation (4)

Source: Internet
Author: User
Our index.php file looks like this:

Here is the 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 tips for using the controller, which you can do in PHP:

$this->{$_get[' method ']} ($_get[' param ');


One suggestion is that you better define the namespace form (namespace) of the program URL, so it will compare specifications such as:

Here is the code snippet:

"Index.php?class=productview&method=productitem&id=4"


Through it we can handle our controller in this way:

Here is the code snippet:

$view =new $_get[' class ');
$view->{$_get[' method '] ($_get[' id ');


Sometimes it's hard to build a controller, such as when you weigh the speed and adaptability of your development. A good place to get inspired is the Java Struts of the Apache group, whose controllers are completely defined by the XML document.

  • Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.