php Laravel - 加载常见的页眉和页脚以查看

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/41074473/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-26 02:19:02  来源:igfitidea点击:

Laravel - Load common header and footer to view

phplaravellaravel-5.1

提问by KikA R

I am new to laravel and I am trying to load header, footer and the view file from controller in a common template and display the data from controller in the view file. But I am get error View ['admin.dashboard'] not found.

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

The dashboard file is present in admin folder inside views

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

controller

控制器

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'));

   }
}

common_template.blade View

common_template.blade 查看

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

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

When I add 'admin/dashboard' instead of $data['template']directly in $templateit loads the dashboard file whereas it doesnt load when i pass it as string from controller.

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

dashboard.blade view

仪表板.刀片视图

<p><?php echo $data['title']; ?></p> //printing the data from the controller

Kindly help me get through this. Thanks

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

回答by Alexey Mezenin

To include blade template into another template, use @include:

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

@include('admin.dashboard')

Or

或者

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

Also, check if view has correct name and is in right directory:

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

resources/views/admin/dashboard.blade.php

回答by AddWeb Solution Pvt Ltd

First of all your code require correction as per the laravel blade code standard. Try below code:
common_template.blade View

首先,您的代码需要根据 laravel 刀片代码标准进行更正。试试下面的代码:
common_template.blade View

@include('includes.header')

@yield('content')

@include('includes.footer')

dashboard.blade view

仪表板.刀片视图

@extends('common_template')

@section('content')
    {{$data['title']}}
@endsection

回答by Balaji Rajendran

To include blade template into another template,

layouts/index.blade.php

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

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>

layouts/footer.blade.php

布局/footer.blade.php

// footer code
<h1>This area for footer code

layouts/header.blade.php

布局/header.blade.php

// headercode
<h1>This area for headercode

/home.blade.php

/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