Laravel 5.7 - Side bar control via Controller

Asked

Viewed 88 times

2

I’m developing a site that contains a sidebar that will show some information to users. This information is in a database, so I have to access it via controller and then send it to view. It turns out that this sidebar is shown on all routes of the site

What is the best way to solve this, without having to repeat the same code, or calling a method several times at all controllers of the site?

Main template:

<!DOCTYPE html>
<html>
<head>    
@include('layouts/header')    
<div class="container" style="padding-bottom: 80px; margin-top: 10px;">
    <div class="row">
        <div class="col-md-3">
            @include('layouts/lateral') <!--Barra lateral comum a todos -->
        </div>

        <div class="col-md-9">
            @yield('content')   
        </div>
    </div>

</div>

@include('layouts/footer')

@yield('script')

Sidebar:

<div class='card'>
    <div class="card-header">
        Informações Gerais
    </div>

    <div class="card-body">
        <strong>Contas cadastradas: </strong> 1<br> <!-- Vem do banco -->
        <strong>Player online: </strong> 0 <br> <!-- Vem do banco -->
        <hr>
    </div>
</div>
  • Because it doesn’t put in Main template?

  • It’ll be the same. Anyway, I’ll have to keep repeating the same code in all methods of all controllers that return a view.

1 answer

2


The technique created to do this is with Viewcomposers which are data that can be shared in several Views of its one-place system. The Viewcomposers is a way to create only one data return and send that information to all Views which can be configured for some, several or all of your Views.

Google Translation Display composers are call returns or class methods that are called when an display is rendered. If you have data that you want to link to a view whenever this view is rendered, a visualization composer organizes this logic in a single location.

Example:

Create a folder inside app\Http by the name of ViewComposers, and create a class inside with the suggested name SideBarComposer with the following code:

<?php namespace App\Http\ViewComposers;

use Illuminate\View\View;

class SideBarComposer
{    

    public function __construct()
    {
    }

    public function compose(View $view)
    {
        $contas_cadastradas = 1;
        $jogadores_online = 1;
        $view->with('contas_cadastradas', $contas_cadastradas);
        $view->with('jogadores_online', $jogadores_online);
    }
}

where the values 1 in each variable would be the return of its database for example, can inject by the constructor the models no problems or use any information recovery technique.

Abra AppServiceProvider which is located in the folder app\Providers and within the method boot add the following line:

public function boot()
{
    ViewComposer::composer('*', \App\Providers\SideBarComposer::class);
}

Observing: don’t forget to add the namespace use Illuminate\Support\Facades\View; to access the method ViewComposer::composer.

this configures the created service to be loaded, and in the settings was placed a * which means that SideBarComposer will rise every time the information that is shared on all pages.

Note: There is no reason why this information should go up to certain pages array with the names of their Views, but from what I read in the question are for all pages.

Finishing now just configure in your view the following code:

<div class='card'>
    <div class="card-header">
        Informações Gerais
    </div>

    <div class="card-body">
        <strong>Contas cadastradas: </strong> {{$contas_cadastradas}}<br>
        <strong>Player online: </strong> {{$jogadores_online}} <br>
    </div>
</div>

The complete example is on the Laravel Framework website, follow the corresponding version of your Laravel.

Browser other questions tagged

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