Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Laravel - Load common header and footer to view

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.

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

<?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.

dashboard.blade view

<p><?php echo $data['title']; ?></p> // Printing the data from the controller
like image 249
KikA R Avatar asked Dec 10 '16 10:12

KikA R


People also ask

How do you add common header and footer in Laravel?

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.

How do I load a view in Laravel?

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.

How do you open the header and footer toolbar?

1. On Word Menu Toolbar → Select VIEW → select Header and Footer. 2. The Header and Footer toolbar opens.


3 Answers

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
like image 113
Alexey Mezenin Avatar answered Sep 17 '22 02:09

Alexey Mezenin


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
like image 28
AddWeb Solution Pvt Ltd Avatar answered Sep 21 '22 02:09

AddWeb Solution Pvt Ltd


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
like image 26
Balaji Rajendran Avatar answered Sep 17 '22 02:09

Balaji Rajendran