2014-09-04 30 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> 
相關問題