簡體   English   中英

在laravel 5中包含控制器

[英]include controller in view laravel 5

我是laravel的新手,但我被卡住了。

我的問題是我想要2個部分(導航,內容)具有動態數據

這是一些代碼Main Blade

    <!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Portfolio</title>
</head>
    <body>
        <div class="navigation">
            @yield('menu')
        </div>
        <div class="content">
            @yield('content')   
        </div>
    </body>
</html>

投資組合刀片

    @extends('main')

@section('content')
    @foreach($data as $portfolio)
        <a href="portfolio/{!!$portfolio->portfolio_url!!}"><img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/></a>
    @endforeach
@stop

和我的導航刀片

@extends('main')
@section('menu')
    @foreach($menuknoppen as $menuknop)
            <a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
    @endforeach
@stop

投資組合刀片具有控制器,但菜單刀片也具有控制器

EDIT1:

問題是即使添加靜態文本也不會顯示導航

EDIT2:

我的控制器我的投資組合控制器

    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    public function index(){
        //here comes a whole list with what i've done
        $results = DB::table('projects')->get();
        //return $results;
        $data = array();
        foreach ($results as $key => $result) {
            $data[] = $result;
        }
        return view('portfolio.portfolio')->with('data', $data);
    }
    public function getProject($portfolio_url){
        //this gets the project thats clicked
        $results = DB::select('select * from projects where portfolio_url = ?', array($portfolio_url));
        return view('portfolio.single')->with('data', $results['0']);
    }

}

我的導航控制器

class menuController extends Controller {

    /**
     * Show the profile for the given user.
     *
     * @param  int  $id
     * @return Response
     */
    // public function __construct($table){
    //  $results = DB::table($table)->get();

    //     return view('menu')->with('menuknoppen', $results);
    // }
    public function index(){
        $results = DB::table('navigation')->get();

        return view('menu')->with('menuknoppen', $results);
    }

}

您的主刀片應該是:

<!DOCTYPE html>
<html lang="en">

<head>
 <meta charset="UTF-8">
 <title>Portfolio</title>
</head>

<body>
    <div class="navigation">
        @include('menu');
    </div>
    <div class="content">
        @yield('content')   
    </div>
</body>
</html>

您的投資組合應為:

@extends('main')

@section('content')
  @foreach($data as $portfolio)
   <a href="portfolio/{!!$portfolio->portfolio_url!!}"><img src='{{ URL::asset("images/$portfolio->picture.jpg") }}'/></a>
  @endforeach

@stop

導航字段應為:

//Don't use extends here
 @foreach($menuknoppen as $menuknop)
        <a href='{{ URL::to("$menuknop->menu_url") }}'>{{$menuknop->menutitle}}</a>
@endforeach

傳遞多個數據

public function index()
{
  $data = //data code;
  $results = // results code
  return view(portfolio.portfolio, compact('data', 'results'));
}

您已經混合了@yield,@include和@extend的概念

@yield提供了一個替換的地方,因此當您在其他視圖中調用@extend時,可以在擴展的視圖中重用模板,並用@yield替換零件

@include表示這部分代碼始終被其定義的視圖替換

因此,在設計網頁時,需要確保“始終被調用”(使用@include)和可以替換的內容(使用@yield)。

作為對aldrin27工作代碼的輔助說明,我希望這能使您對刀片模板更加清楚,這真是令人震驚! :d

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM