← Back to PHP Course | Chapter 14: Advanced PHP | Lesson 10 of 24

PHP MVC Architecture

MVC एक program को तीन helpers में split करता है: एक data रखता है, एक page दिखाता है, और एक traffic direct करता है। यह एक kitchen, एक dining room, और एक waiter वाले restaurant जैसा है।

MVC क्या है?

MVC किसी application को तीन responsibilities में split करता है -- Model (data और business rules), View (presentation), और Controller (request handling) -- ताकि data कैसे दिखता है उसमें एक change यह touch किए बिना हो सके कि यह कैसे store या validate होता है।

उदाहरण: What is MVC?

php
<?php
// Define the class `UserModel`
class UserModel {
    // Define the function `getName` with no parameters
    function getName() { return "Alice"; }
}
// Define the class `UserView`
class UserView {
    // Define the function `render` taking `$name`
    function render($name) { echo "<p>Name: $name</p>"; }
}
// Define the class `UserController`
class UserController {
    // Define the function `show` with no parameters
    function show() {
        // Create a new `UserModel` instance, stored in `$model`
        $model = new UserModel();
        // Create a new `UserView` instance, stored in `$view`
        $view = new UserView();
        $view->render($model->getName());
    }
}
(new UserController())->show();
?>

Model Layer

Model layer आपके data की owner है: यह database से बात करती है, business rules enforce करती है, और HTML या HTTP के बारे में कुछ नहीं जानती, जो आपके data logic को किसी एक web request के बाहर भी reusable रखता है।

उदाहरण: The Model Layer

php
<?php
// Define the class `UserModel`
class UserModel {
    // Define the function `findById` taking `$id`
    function findById($id) {
        // Return `["id" => $id, "name" => "Alice"]` from this function
        return ["id" => $id, "name" => "Alice"];
    }
}
// Create a new `UserModel` instance, stored in `$model`
$model = new UserModel();
// Print a human-readable dump of `$model->findById(1)`
print_r($model->findById(1));
?>

Controller Layer

Controller एक incoming request receive करता है, उपयुक्त Model से data माँगता है, और decide करता है कि कौन सा View response render करे -- यह coordination layer है, जहाँ business logic या markup नहीं belong करता।

उदाहरण: The Controller Layer

php
<?php
// Define the class `UserModel`
class UserModel {
    // Define the function `find` taking `$id`
    function find($id) { return ["name" => "Alice"]; }
}
// Define the class `UserController`
class UserController {
    // Define the function `show` taking `$id`
    function show($id) {
        // Create a new `UserModel` instance, stored in `$model`
        $model = new UserModel();
        // Declare `$data`, set to `$model->find($id)`
        $data = $model->find($id);
        // Print `"Rendering view with: " . $data['name']` to the output
        echo "Rendering view with: " . $data['name'];
    }
}
(new UserController())->show(1);
?>

View Layer

View layer का इकलौता काम presentation है: यह Controller द्वारा दिया गया data लेकर इसे HTML में render करता है, ideally display के लिए loops और conditionals से आगे कोई real logic रखे बिना।

उदाहरण: The View Layer

php
<?php
// Define the class `UserView`
class UserView {
    // Define the function `render` taking `$data`
    function render($data) {
        // Loop over `$data`, binding each item to `$key => $value`
        foreach ($data as $key => $value) {
            // Print "$key: $value\n" to the output
            echo "$key: $value\n";
        }
    }
}
(new UserView())->render(["name" => "Alice", "age" => 30]);
?>

MVC में Routing

एक router incoming URL examine करता है और decide करता है कि कौन सी Controller action इसे handle करे, जो है कि एक MVC app raw script paths के बजाय /users/5/edit जैसे clean, predictable URLs expose कैसे कर पाता है।

उदाहरण: Routing in MVC

php
<?php
// Declare `$url`, set to "/users/5/edit"
$url = "/users/5/edit";
// Check whether `preg_match('#^/users/(\d+)/edit$#', $url, $matches)`
if (preg_match('#^/users/(\d+)/edit$#', $url, $matches)) {
    // Print "Routing to UserController::edit(" . $matches[1] . ")" to the output
    echo "Routing to UserController::edit(" . $matches[1] . ")";
}
?>
Related Topics
{# common_mistakes/chapter_summary/browser_support: on Hindi pages the view already swaps in the hi_ translation fields (or blanks these out if untranslated), so this renders correctly for both languages without a lang_code check here. #}
आम गलतियां
  1. view templates के अंदर SQL और business logic डालना।
  2. controller से सब कुछ करवाना, ताकि यह विशाल और maintain करना मुश्किल हो जाए।
  3. model को HTML output करने देना, जो data और presentation को mix कर देता है।

Login to run this code

C/C++/Java/PHP execution requires a free account. Your code is saved — you'll land right back in the editor after logging in.