PHP MVC Architecture
In this page:
MVC क्या है?
MVC किसी application को तीन responsibilities में split करता है -- Model (data और business rules), View (presentation), और Controller (request handling) -- ताकि data कैसे दिखता है उसमें एक change यह touch किए बिना हो सके कि यह कैसे store या validate होता है।
उदाहरण: What is MVC?
<?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();
?>
Login to try C/C++/Java/PHP code in the editor
Model Layer
Model layer आपके data की owner है: यह database से बात करती है, business rules enforce करती है, और HTML या HTTP के बारे में कुछ नहीं जानती, जो आपके data logic को किसी एक web request के बाहर भी reusable रखता है।
उदाहरण: The Model Layer
<?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));
?>
Login to try C/C++/Java/PHP code in the editor
Controller Layer
Controller एक incoming request receive करता है, उपयुक्त Model से data माँगता है, और decide करता है कि कौन सा View response render करे -- यह coordination layer है, जहाँ business logic या markup नहीं belong करता।
उदाहरण: The Controller Layer
<?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);
?>
Login to try C/C++/Java/PHP code in the editor
View Layer
View layer का इकलौता काम presentation है: यह Controller द्वारा दिया गया data लेकर इसे HTML में render करता है, ideally display के लिए loops और conditionals से आगे कोई real logic रखे बिना।
उदाहरण: The View Layer
<?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]);
?>
Login to try C/C++/Java/PHP code in the editor
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
// 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] . ")";
}
?>
Login to try C/C++/Java/PHP code in the editor
- view templates के अंदर SQL और business logic डालना।
- controller से सब कुछ करवाना, ताकि यह विशाल और maintain करना मुश्किल हो जाए।
- model को HTML output करने देना, जो data और presentation को mix कर देता है।
Chapter Quiz — Complete all 24 topics to unlock
0/24 topics done
Complete these topics first:
- PHP Date & Time
- PHP Math Functions
- PHP JSON Handling
- PHP XML Handling
- PHP cURL Introduction
- PHP REST API Basics
- PHP Composer & Packages
- PHP Autoloading
- PHP Design Patterns
- PHP MVC Architecture
- PHP Security Best Practices
- PHP Performance Optimization
- PHP 8 New Features
- PHP Type Declarations
- PHP Match Expression Advanced
- PHP Fibers
- PHP Attributes
- PHP Magic Constants
- PHP Include & Require
- PHP Iterables
- PHP SimpleXML Parser
- PHP SimpleXML Get
- PHP XML Expat Parser
- PHP DOM Parser