gpt4 book ai didi

php - 在 MVC 应用程序中生成路由的最佳位置在哪里?

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

假设您有以下场景:

In you application, you have several models that are "Commentable". Comments are shown inan identical manner and can thus utilize the same template. In the default view of thecomments widget, the two most recent comments are shown, with a link to load remaining Xcomments.

The widget also have a form (i.e textarea that submits on enter-key) for adding a newcomment to the model.

鉴于上述要求,哪里是生成表单和加载链接完成其工作所需的链接的合理位置?

是否应该在调用模板的 View 中生成链接以及 Commentable 模型?即

<?php
echo $this->partial('path/to/template/comments.phtml', array (
'add-link' => $this->url($params, $routeName),
'load-link' => $this->url($params, $routeName),
'comments' => $this->model->getComments()
);

Commentable 询问这些链接是否完全可以接受?即在 comments.phtml-template 中:

<div class="comments">
<div class="loadLink">
<a href="<?php echo $this->comments->getLoadLink() ?>">
<?php echo sprintf('Show all %d comments', count($this->comments)); ?>
</a>
</div>
<div class="container">
<?php
foreach ($this->comments->getFirst(2) as $comment) {
echo $this->partial('comment.phtml', array('comment' => $comment);
}
?>
</div>
<div class="addComment">
<form action="<?php echo $this->comments->getAddLink() ?>" method="post">
<div>
<textarea class="autogrow" name="comment"
placeholder="Write a new comment" rows="1" cols="80">
</textarea>
</div>
</form>
</div>
</div>

既然 MVC 提倡 View 可以与 Controller 通信,路由生成是否被视为与 Controller “通信”的一种方式,即使它不是以对象到对象的方式这样做?

最佳答案

在 MVC 应用程序中,您永远不应要求模型构建链接。您应该添加一些可以使用该模型生成这些链接的 View 助手。

关于php - 在 MVC 应用程序中生成路由的最佳位置在哪里?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/7895756/

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