@Yield does not display content

Asked

Viewed 411 times

2

I am learning Laravel 5.1, I came across a mistake while using the @yield. I can’t move the contents from another archive to the archive where I want to include.

Next I have the content I want to insert in the file default-home.blade.php inside the layouts folder:

@extends('layouts.default-home')
@section('content')      

<section id="feature_two">
<div class="container">
    <div class="row">

        <!-- Feature Two Description -->
        <div id="feature_2_description" class="col-md-5 feature_description triggerAnimation animated" data-animate="fadeInLeft">

            <h2>Super easy to customize and well detailed for beginners</h2>

            <p>Vestibulum at est vel felis adipiscing tincidunt. Proin quis diam ac lectus pretium mollis interdum sed erat. Phasellus eget
                neque eu ipsum laoreet suscipit tincidunt suscipit purus rutrum 
            </p>

            <p>Etiam euismod, ligula nec volutpat tempor, risus lerisque tincidunt purus libero. Fusce tincidunt ligula, nec sagittis turpis</p>

        </div><!-- End Feature Two Description -->

        <!-- Feature Two Image -->
        <div id="feature_2_image" class="col-md-7 feature_image text-right triggerAnimation animated" data-animate="fadeInRight">
            <img class="img-responsive" src="img/thumbs/feature_two_img.png" alt="feature_two_img">
        </div>

    </div><!-- End row -->  
</div><!-- End container -->
</section>

@stop

This is file with the @yield inserted, the @includes are working:

<body class="notransition"  ng-app>

    @include('layouts.head-home')
    @yield('content') 
    @include('layouts.footer-home')

    <div id="status"></div>

</body>

In this case, the site I’m developing uses the links in the menu to jump to another part of the site below, besides this content does not display the console presents this error every time I roll the page or click on the link for scrolling:

Uncaught Typeerror: Cannot read Property 'top' of Undefined

Routes.php file :

<?php

/*
|--------------------------------------------------------------------------
| Application Routes
|--------------------------------------------------------------------------
|
| Here is where you can register all of the routes for an application.
| It's a breeze. Simply tell Laravel the URIs it should respond to
| and give it the controller to call when that URI is requested.
|
*/


Route::get('','Site\HomeController@home');
Route::get('/','Site\HomeController@home');


?>

Controller:

<?php

namespace App\Http\Controllers\Site;


use App\Http\Controllers\Controller;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\DB;


class HomeController extends Controller {

    public function home(){
        return view('site/home'); //chamando a view home.blade.php
    }
}
  • The second file that would be the layouts/default-home.blade.php?

  • Yes, it is the content of the default-home.php file.

  • and how is the file name you want to include? is calling the correct file on your route ?

  • Do I need to call on the route that part of the file? In case everything is on a single page and it happens the jumps to another part. My route file looks like this: Route::get(','Site Homecontroller@home'); Route::get('/','Site Homecontroller@home');

  • I think it’s best to include your Routes.php file in the question, click [Edit] and enter it nicely.

  • Ah! the error is in the controller. Swap the bar for a point. return view('site.home');. Laravel follows this convention for views

Show 1 more comment

1 answer

1


From the comments I noticed some problems that need to be fixed.

In the route file, you set twice a route to Site\HomeController@home. The route '' ends up being unnecessary, so you can eliminate her:

<?php

Route::get('/','Site\HomeController@home');

Another point, your route is pointing to the class HomeController inside the directory app/Http/Controllers/Site. How the Laravel application follows the PSR-4, this controller namespace must contain the end Site:

<?php

namespace App\Http\Controllers\Site;

If you don’t have a good reason to move the controller to a subfolder, keep it the way it is inside the folder app/Http/Controllers.


Fixed these problems, let’s view view.

To return the view with the content on its home page, just return the view path this way in the method home of HomeController:

public function home(){
    return view('main');  
    // caso a view esteja em alguma pasta, utilize o . como separador
    // como você fez com o layouts.default-home
    // return view('pasta.main');  
}

Finally, you can return the page directly from the route files by an anonymous function, thus eliminating the need for the controller in simple pages:

Route::get('/', function () {
    return view('pasta.main');
});
  • even doing so: Return view('site.home'); it does not display content

  • the file home is in that folder?

  • The content I want to display is in a file within Resources/views/layouts/. Named after products.blade.php

  • 1

    view('site.home') will search the archive resources/views/site/home.blade.php. If you want to display the file resources/views/layouts/produtos.blade.php has to return in view view('layouts.produtos)

  • Content that should be inserted into the default-home.blade.php file via @Yield

  • I’m trying to call home with product content by @Yield, is that possible? Because they should be on the same page.

  • Ok, I made the modification to view('layouts.products) as you recommended and it worked. Thank you so much.

  • The layouts.default-home is the template file of your template. You do not call it directly from the view. You need to call a second file that inherits the contents of layouts.default-home, which is what you set out in @extends(layouts.default-home), Got it ?

  • Yes, I understand, but what if I have more views that inherit.default-home layouts, as I should call in Controller?

Show 5 more comments

Browser other questions tagged

You are not signed in. Login or sign up in order to post.