NickZucco's avatar

How to extend a layout.blade.php file from a file stored in a subfolder

Laravel From Scratch - Question about lesson 5 Layouts https://laracasts.com/series/laravel-5-from-scratch/episodes/5

From one of the previous video in the series, I had a pages folder within the views folder, and there I put my about.blade.php file. I also have the welcome.blade.php and layout.blade.php as shown in this video. All the structure would be the following:

->resources ->error ->pages ->about.blade.php ->vendor ->layout.blade.php ->welcome.blade.php

The welcome page works fine, but when I try to load the /about URL, I get a page that duplicates the code from the layout and the section from the welcome file (That means I get two h1 elements with the text "The Welcome Page Goes Here" and two Javascript alerts with the correct text "About page only"). In my about.blade.php file I have the following:

@extends('layout')

@section('content')

The About Page Goes Here

@endsection

So the problem is that somehow the layout file is not being loaded or found properly. I tried creating another about.blade.php file in the views folder, and then it worked properly. Could anyone tell me how to extend a layout file that is stored in an upper folder?

0 likes
10 replies
tykus's avatar

It's hard to understand what your directory structure is -- this is the structure I would expect given the template you describe:

resources
└── views
    ├── about.blade.php
    ├── layout.blade.php
    └── welcome.blade.php

khaledSMQ's avatar

@NickZucco you only need to add dot

like your file in resources/views/my_aswome_views/content.blade.php

in your blade template you need to extend content view add it like this

@extends('my_aswome_views.content')
NickZucco's avatar

I would like to add an image of my current structure, but I can't seem to find a way to do it. I tried drag and drop and that didn't work.

Also (sorry for my newbness) how can I add those blackboxes with code? Using HTML?

NickZucco's avatar

This would be my actual folder structure, notice how the about view is inside a pages folder, instead of just being inside the views folder.


resources
└── views
    └──pages
        └── about.blade.php
    └──layout.blade.php
    └── welcome.blade.php

@khaledSMQ thanks for your reply, but in this case I don't want to extend the about view, rather the layout one. So, in the about.blade.php I tried:


@extends('layout')

That means, from a view inside my pages folder (according to my structure) I'm trying to extend another view that is outside, just in the views folder. Any further help would be most appreciated.

khaledSMQ's avatar

okay look for this example

// this is your master layout
<html>
    <head>
        <title>App Name - @yield('title')</title>
    </head>
    <body>
        @section('sidebar')
            This is the master sidebar.
        @show

        <div class="container">
            @yield('content')
        </div>
    </body>
</html>

// this is your about page

@extends('layout')

@section('title', 'About ME')

@section('sidebar')
    @parent
    <p>This is appended to the master sidebar.</p>
@endsection

@section('content')
    <p>This is my body content.</p>
@endsection

NickZucco's avatar

Thanks again Khaled. I tried your example but it didn't result as expected.

My conclusion:

Using Laravel 5.2, you can only expand a layout file correctly when both files (in my case: layout and about) are in the views folder, and not within another folder inside views.

khaledSMQ's avatar

Hi , from my understanding your folders like this.

resources
└── views
    └──pages
        └── about.blade.php
    └──layout.blade.php
    └── welcome.blade.php

means your about page it's inside pages folder, and the layout inside view folder means in your route.php or controller you need only to return the about like this

 return view('pages.about');
NickZucco's avatar

Yeah, that's exactly what I was trying in my controller, which seemed quite logical.

But when actually seeing the webpage I get a content that doesn't correspond to the code present in my about view.

Then I moved the about view out of that pages folder and pasted it just in the views folder and did this


return view('about');

With that it worked perfectly... So I really cannot understand the reason for this. I also have this welcome.blade.php as you can see. When the about view is inside the pages folder, and I load in my browser the /about URL, the content corresponds to the content of the welcome view and not the about view. So weird.

khaledSMQ's avatar

i am not sure you, Might already you have duplicate the route get method more than one time for the same URL with different view respond or you need to clear view cache memory by running in terminal

php artisan view:clear

Please or to participate in this conversation.