Php 如何使用细枝递归显示注释?
我正在使用Silex框架重写一个应用程序。在这个应用程序中,用户可以对帖子和评论进行评论。在非MVC应用程序中,受启发,我这样写:Php 如何使用细枝递归显示注释?,php,twig,silex,Php,Twig,Silex,我正在使用Silex框架重写一个应用程序。在这个应用程序中,用户可以对帖子和评论进行评论。在非MVC应用程序中,受启发,我这样写: function display_comments($postid, $parentid=0, $level=0){ // Get the current comment from DB and display with HTML code display_comments($needid, $comment['id'], $level+1); } 但
function display_comments($postid, $parentid=0, $level=0){
// Get the current comment from DB and display with HTML code
display_comments($needid, $comment['id'], $level+1);
}
但是,在Silex应用程序中,我希望从存储库中的数据库中检索它们的注释,将其发送到控制器中的细枝模板,最后在模板中显示HTML代码。这使得以前的解决方案不兼容
Silex中解决此问题的好方法是什么?我应该在视图中放置什么,在控制器中放置什么,在模型中放置什么
编辑
我现在在控制器中编写了函数:
$app->get('/needdetail/{id}', function ($id) use ($app) {
$need = $app['need']->findNeed($id);
function display_comments($app, $needid, $comments=array(), $parentid=0, $level=0){
$replies = $app['comment']->findByNeed($needid, $parentid);
foreach($replies as $reply){
$reply['level'] = $level;
array_push($comments, $reply);
display_comments($app, $needid, $comments, $reply['id'], $level+1);
}
return $comments;
}
return $app['twig']->render('needdetail.html', array('need' => $need, 'comments' => display_comments($app, $id)));
})
现在显示了级别0的注释,但没有显示更深级别的注释。我使用稍微不同的方法获得了所需的结果。控制器和视图都包含一个递归函数: 控制器:
$app->get('/needdetail/{id}', function ($id) use ($app) {
$need = $app['need']->findNeed($id);
function get_comments($app, $needid, $parentid=0){
$comments = array();
$replies = $app['comment']->findByNeed($needid, $parentid);
foreach($replies as $comment){
$comment['replies'] = get_comments($app, $needid, $comment['id']);
array_push($comments, $comment);
}
return $comments;
}
return $app['twig']->render('needdetail.html', array('need' => $need, 'comments' => get_comments($app, $id)));
})
{% for comment in comments %}
{% include 'comment.html' with {'level': 0} %}
{% endfor %}
<div class="comment">
//Comment HTML
</div>
{% if comment.replies %}
{%for reply in comment.replies %}
{% include 'comment.html' with {'comment': reply, 'level': level+1} %}
{% endfor %}
{% endif %}
查看:
$app->get('/needdetail/{id}', function ($id) use ($app) {
$need = $app['need']->findNeed($id);
function get_comments($app, $needid, $parentid=0){
$comments = array();
$replies = $app['comment']->findByNeed($needid, $parentid);
foreach($replies as $comment){
$comment['replies'] = get_comments($app, $needid, $comment['id']);
array_push($comments, $comment);
}
return $comments;
}
return $app['twig']->render('needdetail.html', array('need' => $need, 'comments' => get_comments($app, $id)));
})
{% for comment in comments %}
{% include 'comment.html' with {'level': 0} %}
{% endfor %}
<div class="comment">
//Comment HTML
</div>
{% if comment.replies %}
{%for reply in comment.replies %}
{% include 'comment.html' with {'comment': reply, 'level': level+1} %}
{% endfor %}
{% endif %}
Comment.html:
$app->get('/needdetail/{id}', function ($id) use ($app) {
$need = $app['need']->findNeed($id);
function get_comments($app, $needid, $parentid=0){
$comments = array();
$replies = $app['comment']->findByNeed($needid, $parentid);
foreach($replies as $comment){
$comment['replies'] = get_comments($app, $needid, $comment['id']);
array_push($comments, $comment);
}
return $comments;
}
return $app['twig']->render('needdetail.html', array('need' => $need, 'comments' => get_comments($app, $id)));
})
{% for comment in comments %}
{% include 'comment.html' with {'level': 0} %}
{% endfor %}
<div class="comment">
//Comment HTML
</div>
{% if comment.replies %}
{%for reply in comment.replies %}
{% include 'comment.html' with {'comment': reply, 'level': level+1} %}
{% endfor %}
{% endif %}
//注释HTML
{%if comment.repress%}
{%用于评论中的回复。回复%}
{%include'comment.html'和{'comment':回复,'level':level+1}%}
{%endfor%}
{%endif%}
从控制器递归获取注释结构,然后将其传递给要显示的视图。我尝试了这样做,但现在出现了“Undefined variable:app”错误…您的函数没有返回任何数据也将pass$app
作为参数:显示注释($app,$id)代码>我现在返回数据并将$app作为参数传递。现在会显示级别0的注释,但不会显示更深级别的注释。