Sidebar via Controller - Laravel 5.1 + AdminLTE 2

0

Good afternoon! I have the following problem here ... In my view template app.blade.php I have the line @include('partials.sidebar') that includes my sidebar, ok. The question is that I have 2 different sidebar, one for each "type of register", so I wanted to do this include dynamically by my controller, would it be possible?

I would like to pass +/- this:

return view('inicio')->with(['sidebar' => 'partials.sidebarAdmin']);

or

return view('inicio')->with(['sidebar' => 'partials.sidebarUser']);

as defined in the controller.

But if I put @include($sidebar) in my view app.blade.php , it returns the following error:

Undefined variable: sidebar (View: D:\App\resources\views\app.blade.php) 

How could I pass this data to the view?

Note: Yes, my "start" view has @extends('app') working correctly.

    
asked by anonymous 23.12.2015 / 19:46

2 answers

1

The solution I found was working via GET , passing the admin and other user parameters.

Creating a Route

The first thing I did was create a route for the template , which gets a GET parameter containing the current user, then I direct the logic to a controller method called treatingOSideBarViaGET.php , which in turn calls the page method:

<?php
Route::get('suaURLPreferida/{whichUser}', 'tratandoOSideBarViaGET@page');

Generating the Controller

Now what's the fun part: D

In controller I define that page method that receives a parameter whichUser is responsible for returning the current user. Then inside the method I point to views and search for the template inicio.blade.php , in it I define a parameter php artisan make:controller tratandoOSideBarViaGET , responsible for knowing if we are making a call from Admin or User :

<?php
namespace App\Http\Controllers;

use Illuminate\Http\Request;

use App\Http\Requests;
use App\Http\Controllers\Controller;

class tratandoOSideBarViaGET extends Controller
{
    public function page($whichUser)
    {
        return view('inicio')->with('user', $whichUser);
    }
}

Template

Finally, in the template I create a condition, if App\Http\Controllers\ is admin then $user , or if $user is user then @include('sidebarAdmin') , if both return $user does not show anything:

@if ($user == 'admin')
    @include('sidebarAdmin')
@elseif ($user == 'usuario')
    @include('sidebarUser')
@endif

Final considerations

Realize that this is not the only way to do this, but it would be a good practice to follow through with it. Understand you might as well do so:

<?php
Route::get('suaURLPreferida/{whichUser}', function ($whichUser) {
    return view('inicio')->with('user', $whichUser);
});

I do not recommend this, because you are mixing the functions of the MVC, it is not the function of a route add a parameter to a view controller so I separated the responsibilities. Feel free to choose the best solution, just wanted to make my point clear.

    
23.12.2015 / 21:33
2

Your syntax is incorrect.

The correct one would be ...

return view('inicio')->with('sidebar' => 'partials.sidebarAdmin');
return view('inicio')->with('sidebar' => 'partials.sidebarUser');

Or even

$sidebar = 'partials.sidebarUser';
return view('inicio', compact('sidebar'));

Or even ...

return view('inicio', ['sidebar' => 'partials.sidebarUser']);

As for the condition for displaying different sidebars, you can do either in the controller, the view, or even using view composers .

    
23.12.2015 / 19:58