页面控制器设计模式是基于 web 的系统中使用的常见架构方法。它通过专用特定控制器来处理单个页面或请求的逻辑来组织控制流。这种方法有助于隔离职责,使代码库更易于维护和发展。
什么是页面控制器?
在页面控制器模式中,每个页面(或一组具有类似行为的页面)都有自己的控制器,负责:
- 处理请求:处理客户端发送的数据。执行页面特定逻辑:验证输入、与模型交互或执行计算。渲染响应:将处理后的数据传递到视图(模板)并将最终响应返回给客户端。
该模式的优点
- 简单流程:每个页面都映射到自己的专用控制器。关注点分离:每个控制器只处理自己的逻辑。可维护性:对一个页面的更改仅影响其关联的控制器。可扩展性:添加新页面非常简单,并且不会破坏现有功能。
基本结构
典型的实现涉及以下组件:
控制器:包含特定页面逻辑的php文件。路由:将 url 映射到控制器的路由机制。视图:用于渲染用户界面的模板。
流动
- 客户端向特定 url 发送请求。路由系统为请求识别适当的控制器。控制器执行所需的逻辑并将响应渲染委托给视图。视图生成最终输出并将其返回给客户端。
实施例
文件结构
/htdocs /src /controllershomecontroller.phpaboutcontroller.php /servicesviewrenderer.php /viewshome.html.phpabout.html.php /public index.php /routes.php poser.json
自动加载器
{ "autoload": { "psr-4": {"app\": "htdocs/" } }}
poser dump-autoload
模板
主页和about.html.php.
的模板
<!doctype html><html><head> <title><?= htmlspecialchars($title) ?></title></head><body> <h1><?= htmlspecialchars($title) ?></h1> <p><?= htmlspecialchars($content) ?></p></body></html>
viewrenderer
namespace appservices;class viewrenderer { public function render(string $view, array $data = []): void { extract($data); // turns array keys into variables include __dir__ . "/../../views/{$view}.html.php"; }}
homecontroller
处理主页逻辑。
namespace appcontrollers;use appservicesiewrenderer;class homecontroller { public function __construct(private viewrenderer $viewrenderer) { } public function handlerequest(): void { $data = [‘title’ => ‘wele to the site’,’content’ => ‘homepage content.’, ]; $this->viewrenderer->render(‘home’, $data); }}
关于控制器
处理“关于我们”页面逻辑。
namespace appcontrollers;use appservicesiewrenderer;class aboutcontroller{ public function __construct(private viewrenderer $viewrenderer) { } public function handlerequest(): void { $data = [‘title’ => ‘about us’,’content’ => ‘information about the pany.’, ]; $this->viewrenderer->render(‘about’, $data); }}
routes.php
定义到控制器的路由映射。
use appcontrollershomecontroller;use appcontrollersboutcontroller;// define the routes in an associative arrayreturn [ ‘/’ => homecontroller::class, ‘/about’ => aboutcontroller::class,];
index.php
应用程序的入口点。
require_once __DIR__ . ‘/../vendor/autoload.php’;use AppServicesViewRenderer;// Include the routes$routes = require_once __DIR__ . ‘/../routes.php’;// Instantiate the view rendering service$viewRenderer = new ViewRenderer();// Get the current route from the request URI$requestUri = parse_url($_SERVER[‘REQUEST_URI’], PHP_URL_PATH);// Check if the route exists and resolve the controllerif (isset($routes[$requestUri])) { $controllerClass = $routes[$requestUri]; $controller = new $controllerClass($viewRenderer); $controller->handleRequest();} else { http_response_code(404); echo "Page not found.";}
优点和缺点
优点
组织:控制器是模块化的,每个控制器处理一个特定的页面。可重用性:视图可以在不同的控制器之间重用。调试:由于每个页面都有自己的专用控制器,因此更容易跟踪错误。
缺点
控制器数量增加:大型项目可能导致控制器激增,需要更好的组织。代码重复:控制器之间的通用逻辑可能会重复。这可以通过使用基本控制器类来缓解。何时使用页面控制器模式?简单系统:最适合每个页面都有特定逻辑的中小型 web 应用程序。模块化项目:当您想要隔离逻辑以便于维护时。没有框架:非常适合没有强大框架(如 laravel 或 symfony)的 php 项目。
对于更复杂的项目,存在大量逻辑重用或多个入口点,前端控制器或完整mvc架构等模式可能更合适。
以上就是PHP 设计模式:页面控制器的详细内容,更多请关注范的资源库其它相关文章!
转载请注明:范的资源库 » PHP设计模式:页面控制器