我是 Laravel 的新手,我正在尝试在通用模板中从控制器加载页眉、页脚和视图文件,并在视图文件中显示来自控制器的数据。但我收到错误
未找到视图['admin.dashboard']。
仪表板文件位于视图内的管理文件夹中。
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'));
}
}
<?php echo View::make('includes/header'); ?>
<?php echo $template = "'".$data['template']."'";
echo View::make($template); ?>
<?php echo View::make('includes/footer'); ?>
当我直接在
$data['template']
中添加“admin/dashboard”而不是 $template
时,它会加载仪表板文件,而当我从控制器将其作为字符串传递时,它不会加载。
<p><?php echo $data['title']; ?></p> // Printing the data from the controller
首先,您的代码需要根据 Laravel Blade 代码标准进行更正。尝试以下代码:
common_template.blade 视图
@include('includes.header')
@yield('content')
@include('includes.footer')
仪表板.刀片视图
@extends('common_template')
@section('content')
{{$data['title']}}
@endsection
要将 Blade 模板包含到另一个模板中,
布局/index.blade.php
<!DOCTYPE html>
<html lang="en" class="no-js">
<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>
布局/footer.blade.php
// Footer code
<h1>This area for footer code
布局/header.blade.php
// Header code
<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
您可以使用此两个选项制作公共视图。
在 resources/views/includes/ 中创建“includes”文件夹
此文件应在包含文件夹之外创建
home.blade.php 内部
选项1:使用include方法
@include('includes.header')
<Add home page code here>
@include('includes.footer')
选项2:使用扩展方法
template.blade 文件内
@include('includes.header')
@yield('content')
@include('includes.footer')
@extends('template') @section('content') {{$data['name']}} @endsection
在此方法中,@section将在模板内容部分使用yield方法调用并替换内容。