Laravel - 加载通用页眉和页脚以查看
Laravel - Load common header and footer to view
我是 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'));
}
}
common_template.blade 查看
<?php echo View::make('includes/header'); ?>
<?php echo $template = "'".$data['template']."'";
echo View::make($template); ?>
<?php echo View::make('includes/footer'); ?>
当我直接在 $template
中添加 'admin/dashboard' 而不是 $data['template']
时,它会加载仪表板文件,而当我将它作为字符串从控制器传递时它不会加载。
dashboard.blade 查看
<p><?php echo $data['title']; ?></p> //printing the data from the controller
请帮助我度过难关。谢谢
要将 blade 模板包含到另一个模板中,请使用 @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
首先,您的代码需要根据 laravel blade 代码标准进行更正。试试下面的代码:
common_template.blade 查看
@include('includes.header')
@yield('content')
@include('includes.footer')
dashboard.blade 查看
@extends('common_template')
@section('content')
{{$data['title']}}
@endsection
要将 blade 模板包含到另一个模板中,
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
我是 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'));
}
}
common_template.blade 查看
<?php echo View::make('includes/header'); ?>
<?php echo $template = "'".$data['template']."'";
echo View::make($template); ?>
<?php echo View::make('includes/footer'); ?>
当我直接在 $template
中添加 'admin/dashboard' 而不是 $data['template']
时,它会加载仪表板文件,而当我将它作为字符串从控制器传递时它不会加载。
dashboard.blade 查看
<p><?php echo $data['title']; ?></p> //printing the data from the controller
请帮助我度过难关。谢谢
要将 blade 模板包含到另一个模板中,请使用 @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
首先,您的代码需要根据 laravel blade 代码标准进行更正。试试下面的代码:
common_template.blade 查看
@include('includes.header')
@yield('content')
@include('includes.footer')
dashboard.blade 查看
@extends('common_template')
@section('content')
{{$data['title']}}
@endsection
要将 blade 模板包含到另一个模板中,
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