如何包含控制器方法';s视图作为Laravel PHP Framework中另一个控制器视图的一部分


How to include a controller method's view as a part of another controller view in Laravel PHP Framework

Halo这里是我的需要;我想在laravel-php框架中包含不同的视图作为不同视图的一部分。

class DashboardController extends BaseController {
  public function comments( $level_1=''){
   // process data according to $lavel_1
    return View::make('dashboard.comments', $array_of_all_comments);
  }
public function replys( $level_2=''){
   // process data according to $lavel_1
  return View::make('dashboard.replys', $array_of_all_replys);
 }

这两个数据现在都可以从访问

www.abc.com/dashboard/comments
www.abc.com/dashboard/replys  

在我看来,我需要的是根据评论id($lavel_2)生成回复

// dashboard/comments.blade.php
 @extends('layout.main')
 @section('content')
 @foreach($array_of_all_comments as $comment)
   comment {{ $comment->data }},
//here is what i need to load reply according to the current data;
//need to do something like this below
 @include('dashboard.replys', $comment->lavel_2) //<--just for demo
  .................
 @stop

在回复中也得到了

@extends('layout.main')
 @section('content')
  // dashboard/replys.blade.php
    @foreach($array_of_all_replys as $reply)
       You got a reply {{ $reply->data }},
         ...........
  @stop

有什么办法让我在《拉拉威尔4》中做到这一点吗?

请帮助我,我想一次加载评论和回放,以后需要通过ajax和单独访问它们

请帮我提前非常感谢

halo我在这里找到了解决方案

我们只需要使用App::make('DashboardController')->reply();并从包括视图文件中删除所有CCD_ 2和CCD_

变化就像这个

 // dashboard/comments.blade.php
 @extends('layout.main')
 @section('content')
 @foreach($array_of_all_comments as $comment)
   comment {{ $comment->data }},
  //<-- here is the hack to include them 
{{-- */echo App::make('DashboardController')->reply($comment->lavel_2);/* --}}
  .................
 @stop
 .............

和in replys现在改为

  // dashboard/replys.blade.php
    @foreach($array_of_all_replys as $reply)
       You got a reply {{ $reply->data }},
         ...........
  @endforeach
   -------------

感谢

您可能想要重新修改视图并规范化数据。评论和回复(可能)是一样的。

如果您创建了一个属于"父级"(另一个Comment模型)且具有许多"子级"(许多Comment模式)的Comment模型,则只需将顶级Comment的parent_id设置为0,并将其设置为另一个Comment的ID即可使其成为Reply。

然后你的刀片视图会做一些类似的事情:

comments.blade.php
@foreach ($comments AS $comment)
    @include( 'comment', [ 'comment' => $comment ] )
@endforeach

comment.blade.php
<div>
    <p>{{{ $comment->message }}}</p>
    @if( $comment->children->count() )
        <ul>
            @foreach( $comment->children AS $comment )
               <li>
                   @include( 'comment', [ 'comment' => $comment ] )
               </li>
            @endforeach
        </ul>
    @endif
</div>