Laravel - 加载公共页眉和页脚以查看

Kik*_*A R 7 php laravel laravel-5.1

我是laravel的新手,我正在尝试从通用模板中的控制器加载页眉,页脚和视图文件,并在视图文件中显示控制器中的数据.但我得到错误View ['admin.dashboard'] not found.

仪表板文件存在于视图中的admin文件夹中

调节器

namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class common extends Controller
{

   public function login()
   {
        $data['title'] = 'Dashboard';
        $data['template'] = 'admin/dashboard';
        return view('common_template', compact('data'));

   }
}
Run Code Online (Sandbox Code Playgroud)

common_template.blade查看

<?php echo View::make('includes/header'); ?>

<?php echo $template = "'".$data['template']."'"; 
echo View::make($template); ?> 
<?php echo View::make('includes/footer'); ?>
Run Code Online (Sandbox Code Playgroud)

当我添加'admin/dashboard'而不是$data['template']直接在$template其中加载仪表板文件,而当我从控制器传递它作为字符串时它不加载.

dashboard.blade视图

<p><?php echo $data['title']; ?></p> //printing the data from the controller
Run Code Online (Sandbox Code Playgroud)

请帮我解决这个问题.谢谢

Ale*_*nin 8

要将刀片模板包含到另一个模板中,请使用@include

@include('admin.dashboard')
Run Code Online (Sandbox Code Playgroud)

要么

@include($data['template']) // This should be the name of template, like 'admin.dashboard', but not path
Run Code Online (Sandbox Code Playgroud)

另外,检查视图名称是否正确并且在正确的目录中:

resources/views/admin/dashboard.blade.php
Run Code Online (Sandbox Code Playgroud)


Add*_*Ltd 6

首先,您的代码需要按照laravel刀片代码标准进行更正。尝试下面的代码:
common_template.blade视图

@include('includes.header')

@yield('content')

@include('includes.footer')
Run Code Online (Sandbox Code Playgroud)

仪表板视图

@extends('common_template')

@section('content')
    {{$data['title']}}
@endsection
Run Code Online (Sandbox Code Playgroud)


Bal*_*ran 6

要将刀片模板包含到另一个模板中,

layouts/index.blade.php

<head>
    <!-- Site Title -->
    <title>{{ $title }}</title> //dynamic title
    <link rel="stylesheet" href="{{ asset('website/css/main.css') }}"> 
@stack('css') //internal css
</head>

<body>
    @include('../website/layouts/header')//include header
    @yield('content')//include content
    @include('../website/layouts/footer') //include footer
    <!-- start footer Area -->
    <!-- End footer Area -->
    <script src="{{asset('website/js/vendor/jquery-2.2.4.min.js ') }}"></script>
    @stack('js')//internal js
</body>

</html>
Run Code Online (Sandbox Code Playgroud)

布局/footer.blade.php

// footer code
<h1>This area for footer code
Run Code Online (Sandbox Code Playgroud)

布局/header.blade.php

// headercode
<h1>This area for headercode
Run Code Online (Sandbox Code Playgroud)

/home.blade.php

<?php $title = "dynamic title"; ?> //title

@extends('layouts/index') //include index page

@Push('css') // this is for internal js
*{
color:black;
}
@endpush

@section('content') //section for content
This area for home page content
@stop // content ended

@Push('js') // this is for internal js
<script>
    $(document).ready(function(){
         var loggedIn={!! json_encode(Auth::check()) !!};
        $('.send').click(function() {
            if(!loggedIn){
                moda.style.display = "block";   
            return false;
            }
        });
    });
@endpush
Run Code Online (Sandbox Code Playgroud)