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 get error
View ['admin.dashboard'] not found.
The dashboard file is present in the admin folder inside views.
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'); ?>
When I add 'admin/dashboard' instead of $data['template']
directly in $template
, it loads the dashboard file whereas it doesn’t load when i pass it as string from controller.
<p><?php echo $data['title']; ?></p> // Printing the data from the controller
Create layout. blade. php file in layout folder or include header and footer which are make in same folder. @include() method is use for include common header and footer and @yield() method add content from all blade files.
Loading a view in the controller is easy. You just need to add `view('viewname')` method while returning from the controller method. `view()` is a global helper in Laravel. In this method, you just need to pass the name of the view.
1. On Word Menu Toolbar → Select VIEW → select Header and Footer. 2. The Header and Footer toolbar opens.
To include blade template into another template, use @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
First of all your code require correction as per the laravel blade code standard. Try below code:
common_template.blade View
@include('includes.header')
@yield('content')
@include('includes.footer')
dashboard.blade view
@extends('common_template')
@section('content')
{{$data['title']}}
@endsection
To include a Blade template into another template,
layouts/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>
layouts/footer.blade.php
// Footer code
<h1>This area for footer code
layouts/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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With