gpt4 book ai didi

PHP MVC,谁拥有 URL?

转载 作者:搜寻专家 更新时间:2023-10-31 22:02:11 25 4
gpt4 key购买 nike

我正在使用 MVC 技术实现一个 PHP 应用程序。加载http://site/photos/25?options的简单介绍,重写为http://site/index.php?page=photos&id=25&options:

  • index.php -> 实例化并调用PhotoController对象
  • PhotoController 实例化 PhotoView 并将正确的 Photo (模型)加载到其中
  • PhotoController 调用 PhotoView->render() 和页眉/页脚 View
  • PhotoView 可以访问 Photo 模型和它需要的其他模型数据

PhotoView 将输出 HTML 并希望超链接到不同的页面。当该 View 生成超链接时,我对哪一层应该拥有 URL 感到困惑。我看到的选项概述如下。请帮助我选择正确的方法。


模型层拥有 URL

Models have a getURL() method

优点:

  • View 基类可以合并 Open Graph 属性
  • PhotoView 可以这样做:$folderHref = $photo->getFolder()->url

缺点:

  • 一些 Controller 没有单一对应的模型,例如 LoginControllerAdminController,我如何获取这些的 URL?

Controller 拥有 URL

Controllers have url and title properties. To create a link, the presenting controller creates target controllers and passes to the view. The view accesses the url and title from the passed object.

优点:

  • 每个 URL 都直接映射到一个 Controller (在 index.php 中),因此反过来看起来很干净

缺点:

  • 需要其他 View URL 的 View 将需要以下丑陋的技巧之一:

    • 查询不相关的 Controller $folderHref = Controllers\FolderController::urlForFolder($photo->getFolder())

    • View 对它们的 Controller 了解太多$folderHref = $photo->getFolder()->getControllerAndSetURL()->getURL()

    • View 与其 Controller 之间的过度通信 $folderHref = $this->delegate->getURLForFolder($photo->getFolder())$adminHref = $this ->delegate->getURLForAdminHref() with delegate 有很多方法


每个人都拥有 URL

A base class OpenGraphObject is the parent class for Controllers AND Models and has a method getURL()

ControllersModels 仅当它可以不带参数运行时才实现它。 (例如,PhotoController 返回 NULL,因为 URL 取决于要显示的 Photo)。

优点:

  • 上述所有优势

缺点

  • 困惑
  • 吊销我的编程许可证

最佳答案

为了分离关注点,我会像这样分解它:

/**
* Responsible for processing and rendering user output
*/
interface View {

public function render();
}

/**
* Used just to further formalize the messages between Photo and View
*/
interface PhotoView extends View {

public function setPhotoData($id, $title, $link);
}

/**
* Among other things knows how to generate URLs to routes
*/
interface Router {

/**
* Returns the URL to route based on route name
* @param string $routeName
* @param array $params
*/
public function urlTo($routeName, $params = null);
}

我不会实现每个类,您可以使用许多现成的解决方案,例如模板引擎和路由器。

基于 id 显示图像的示例 Controller 可以是:

class PhotoController {

public function displayAction($photoId) {
$photo = Photo::findById($photoId);

$templateFile = $this->pathTo('photo-template.php');

//can be a singleton for example, it holds all the routes for the application
//and knows how to render them based on the name and parameters
$router = AppRouter::instance();

return $photo->render(new ConcretePhotoView($templateFile, $router));
}

}

在此示例中,Photo 需要能够从数据库中获取自身,然后将自身呈现给 PhotoView:

class Photo {

private $id;
private $title;
private $sourceLink;

//other methods...

public function render(PhotoView $view) {
$view->setPhotoData($this->id, $this->title, $this->sourceLink);

return $view->render();
}

}

假设 HTML 模板 photo-template.php 如下所示:

<a href="<?= $userLogin['href'] ?>"><?= $userLogin['label'] ?></a>
<a href="<?= $userRegister['href'] ?>"><?= $userRegister['label'] ?></a>
<div>
<img src="<?= $photo['src'] ?>" title="<?= $photo['title'] ?>" />
<a href="<?= $photoLink['href'] ?>"><?= $photoLink['label'] ?></a>
</div>

这个模板需要什么变量应该足够清楚了。里面没有逻辑,只是纯粹的显示,虽然一些基本的控制就可以了。

剩下的就是编写 ConcretePhotoView 代码,它将使用提供的模板文件和路由器来调整领域对象 Photo 和具体 html 模板之间的数据:

class ConcretePhotoView implements PhotoView {

private $templateEngine;
private $router;

public function __construct($templateFile, Router $router) {
$this->templateEngine = new SomeTemplateEngine($templateFile);
$this->router = $router;
}

public function render() {
//router has a route userLogin with no params
$this->templateEngine->assign('userLogin', array(
'href' => $this->router->urlTo('userLogin'),
'label' => 'Login'
));

$this->templateEngine->assign('userRegister', array(
//router has a route named userRegister that requires no params
'href' => $this->router->urlTo('userRegister'),
'label' => 'Register'
));

return $this->templateEngine->render();
}

public function setPhotoData($id, $title, $link) {
$this->templateEngine->assign('photo', array(
'src' => $link,
'title' => $title
));

$this->templateEngine->assign('photoLink', array(
//router has a route named photo that requires an id
'href' => $this->router->urlTo('photo', array('id' => $id)),
'label' => $title
));
}

}

终于要回答“谁拥有 URL?”这个问题了。 - 路由器。这些知识被封装在那里并且特定于应用程序配置。其他对象使用它来生成链接。

关于PHP MVC,谁拥有 URL?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27117991/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com