2014-09-04 33 views
0

这里是我的需要; 我想在laravel php框架工作中包括不同视图的不同视图。如何在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 

有没有什么办法可以在laravel 4上实现这一点?

请帮助我,我想加载两个意见和重放一气呵成,后来需要通过AJAX单独访问他们也

请帮帮我非常感谢你提前

回答

0

晕我找到了解决办法这里

我们需要的是使用App::make('DashboardController')->reply(); 并删除所有@extends@sections从包括视图文件

的变化都是这样

// 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 
............. 

并在回信现在改为

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

感谢

+0

我不能强调如何哈克和坏的做法,这是。您正在利用刀片标签的解析引擎来执行HMVC,Laravel的设计目的是不需要(大部分)。在视图中创建一个新的控制器(并创建'n'个新的控制器,其中'n'是'$ array_of_all_comments的大小],不应该出于任何原因 – Joe 2014-09-04 11:02:21

0

你可能要返工您的意见和规范你的数据评论和回复(可能)是相同的。

如果您制作属于“父母”(另一个评论模型)和拥有多个“子女”(很多评论模型)的评论模型,那么只需将parent_id设置为0作为顶级评论,并将其设置为ID另一个评论,使其成为答复。

然后刀片的意见做这样的事情:

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> 
相关问题