Sidebar via Controller - Laravel 5.1 + Adminlte 2

Asked

Viewed 694 times

0

Good afternoon! I have the following problem here... In my view model app.blade.php have the line @include('partials.sidebar') which includes my sidebar, okay. The point is that I have 2 different sidebar, one for each "type of registration", so I wanted to make this include dynamically by my controller, it would 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, 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 is with the @extends('app') functioning properly.

  • The mistake was another, I was directing the wrong view, not the one I was testing. Sorry and thanks for the attention.

2 answers

2

Your syntax is incorrect.

The right thing 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 else...

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

As for the condition to display different sidebars, you can do either in the controller, in the view or even using view Composers.

  • then, I used the third option, because I pass a message too, but it did not work, continue with the same error.

  • Return view('home', ['message' => 'success', 'sidebar' => 'partials.sidebar']);

  • The mistake was another, I was directing the wrong view, not the one I was testing. Sorry and thanks for the attention.

  • but anyway the syntax was wrong anyway.

  • @Raylansoares quiet my friend.

1


The solution I found was to work via GET, passing the parameters admin and another user.

Creating a Route

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

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

Generating the Controller

Done that I’ll get one controller using the command php artisan make:controller tratandoOSideBarViaGET that will appear in the directory App\Http\Controllers\.

Now that comes the fun part :D

In the controller I define that method page that receives a parameter whichUser responsible for returning the current user. So within the method I point to views and search by template inicio.blade.php, in it I define a parameter $user, responsible for knowing if we are making a call from Admin or of 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 at the template I create a condition if $user for admin then @include('sidebarAdmin'), or if $user for user then @include('sidebarUser'), if they both return false shows nothing:

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

Final considerations

Note that this is not the only way to do this, but it would be good practice for you to follow 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 MVC, it is not a function of a route add a parameter to a view, this is up to the controller so I separated the responsibilities. Feel free to choose the best solution, just wanted to make clear my point.

  • Very good @joão! I goated a lot of this solution, gave me even another idea rs. But tell me something, I really didn’t want to let an if of the view set this up, it might be silly of me, but I don’t think it’s very safe. Would n have a way to leave it in the controller or middleware?

  • @I’m glad you liked my solution. Well, as I said earlier, it’s good practice for you to separate your code into responsibilities. Now you want to use this same Controller or a Middleware to manipulate HTML, goes against this principle understand?

  • @Raylansoares There is no problem with you using "IF" in Views. "IF" is a Blade (Template Engine) syntactic rule, which proposes an alternative way to integrate HTML with PHP, thus leaving them more "clean". So much so that if you repair, the extension of the files are "Blade.PHP", that is, the file is actually PHP and we are all aware that PHP is safe (or almost :D).

  • @Raylansoares The function of BLADE is actually to make a PARSE of this "alternative syntax" for the traditional PHP we know. And another, do you think that Taylor Otwell (creator of Laravel) would allow documentation of something, which he would not consider safe? I don’t think so? hahaha

  • @Anyway I think I’ve written enough kkkkk Now why don’t you take a look at repository I created just for answers here from Stack Overflow? Your question is the first of many hahaha

  • kkk wrote quite yes, but everything was extremely helpful, thank you very much for the explanations @João Paulo Vieira da Silva

Show 1 more comment

Browser other questions tagged

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