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

时间:2016-12-10 10:25:52

标签: php laravel laravel-5.1

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

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

控制器

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查看

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

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

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

dashboard.blade视图

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

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

3 个答案:

答案 0 :(得分:5)

要将刀片模板包含到其他模板中,请使用@include

@include('admin.dashboard')

或者

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

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

resources/views/admin/dashboard.blade.php

答案 1 :(得分:3)

首先,您的代码需要根据laravel刀片代码标准进行更正。请尝试以下代码:
common_template.blade查看

RewriteEngine On
RewriteBase /
# allow my ip to access magento store
RewriteCond %{REMOTE_ADDR} !^177\.18\.228\.58
RewriteCond %{REQUEST_URI} !^/maintenance\.html$
RewriteRule ^(.*)$ https://magentosite.com.br/maintenance.html [R=307,L]

dashboard.blade视图

@include('includes.header')

@yield('content')

@include('includes.footer')

答案 2 :(得分:2)

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

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 code
<h1>This area for footer code

layouts / header.blade.php

// headercode
<h1>This area for headercode

/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