jeroennoten / Laravel-AdminLTE

Easy AdminLTE integration with Laravel
MIT License
3.78k stars 1.08k forks source link

[FEATURE] change preloader image on BuildingMenu #1251

Closed guga121 closed 4 months ago

guga121 commented 5 months ago

Is your request related to a problem? Please describe.

Hello,

I can´t change the preloader image on runtime BuildingMenu

Changing logo_img on menu works great!!

This is my code:

Event::listen(BuildingMenu::class, function (BuildingMenu $event) {
            $user = Auth::user()->id_empresa;
            $empresa = Empresa::where('id',$user)
                        ->first();

            config(['adminlte.logo' => $empresa->name]);
            if($empresa->logo_img != ''){
                config(['adminlte.logo_img' => $empresa->logo_img]);
            }else{
                config(['adminlte.logo_img' => 'images/logo_128.png']);
                //config(['adminlte.logo_img' => 'vendor/adminlte/dist/img/logo_128.png']);
            }            
            config(['adminlte.logo_img_alt' => $empresa->name]);
            config(['adminlte.logo_img_class' => 'brand-image']);

            /*config(['adminlte.preloader' => [
                        'img' =>[
                                    'path' => 'images/logo_128.png',
                                    'alt' => 'NAME'
                                ]
                        ]
                        ]); */

            config(['adminlte.preloader' => [
                            'img' => [
                                'path' => 'images/logo_128.png'
                            ]
                            ]
                ]);        

        });
    }

Describe the solution you'd like

A clear and concise description of what you want to happen or the enhancement you want.

Describe alternatives you've considered

A clear and concise description of any alternative solutions or features you've considered.

Additional context

Add any other context or screenshots about the feature request here.

dfsmania commented 4 months ago

Hi @guga121 , the building menu event was intended for other purposes, it's triggered before the adminlte menu is generated, but after the adminlte configuration is loaded. So, the changes you made on the preloader takes place after it is showed.

Also, to change configuration, always use the full path to the config key you want to change, like:

config(['adminlte.preloader.img.path' => 'https://picsum.photos/300/300' ]);

Now, in my opinion, the best option to achieve what you need is to create a new middleware and put it on the bottom of the web middleware chain. If you are not familiar with the middleware concept in Laravel, please read its documentation: https://laravel.com/docs/10.x/middleware#main-content

php artisan make:middleware SetupAdminlte

This new middleware will take care of setup the adminlte configuration based on the authenticated user. Open the new middleware file and fill out its handle method with something like next:

app/Http/Middleware/SetupAdminlte.php

<?php

namespace App\Http\Middleware;

use Closure;
use Illuminate\Http\Request;
use Illuminate\Support\Facades\Auth;
use Illuminate\Support\Facades\Log;

class SetupAdminlte
{
    /**
     * Handle an incoming request.
     *
     * @param  \Illuminate\Http\Request  $request
     * @param  \Closure(\Illuminate\Http\Request): (\Illuminate\Http\Response|\Illuminate\Http\RedirectResponse)  $next
     * @return \Illuminate\Http\Response|\Illuminate\Http\RedirectResponse
     */
    public function handle(Request $request, Closure $next)
    {
        Log::info('Updating admilte config for user => ' . Auth::user()->name);

        // Get company related to the logged user.

        $empresa = Empresa::where('id', Auth::user()->id_empresa)->first();
        $logo = ! empty($empresa->logo_img)
            ? $empresa->logo_img
            : 'images/logo_128.png';

        // Update adminlte configuration depending on the authenticated user.

        config([
            'adminlte.logo' => $empresa->name,
            'adminlte.logo_img' => $logo,
            'adminlte.img_alt' => $empresa->name,
            'adminlte.preloader.img.path' => 'https://picsum.photos/300/300',
        ]);

        return $next($request);
    }
}

Please, note the logic is just to exemplify an use case, you need to correctly implement your business logic inside the handler, for example, checking the case when there isn't an authenticated user.

Finally, append this middleware on the web stack:

app/Http/Kernel.php

/**
     * The application's route middleware groups.
     *
     * @var array
     */
    protected $middlewareGroups = [
        'web' => [
            \App\Http\Middleware\EncryptCookies::class,
            \Illuminate\Cookie\Middleware\AddQueuedCookiesToResponse::class,
            \Illuminate\Session\Middleware\StartSession::class,
            // \Illuminate\Session\Middleware\AuthenticateSession::class,
            \Illuminate\View\Middleware\ShareErrorsFromSession::class,
            \App\Http\Middleware\VerifyCsrfToken::class,
            \Illuminate\Routing\Middleware\SubstituteBindings::class,

            // NOTE: We added next middleware to take care of Adminlte package setup.

            \App\Http\Middleware\SetupAdminlte::class,
        ],

        'api' => [
            'throttle:api',
            \Illuminate\Routing\Middleware\SubstituteBindings::class,
        ],
    ];
guga121 commented 4 months ago

Hello Diego Smania, thanks a lot for your response. I already find a way to do it, this one, on Appserviceprovider: I would try also, the one you pass me.

public function boot(): void
    {
        Schema::defaultStringLength(191);
        Paginator::useBootstrap();

        //compose all the views....
        view()->composer('*', function ($view) 
        {
            if(isset(Auth::user()->id_empresa)){

            $user = Auth::user()->id_empresa;
            $empresa = Empresa::where('id',$user)
                        ->first();

            config(['adminlte.preloader.img.path' => 'images/logo_128.png',
                    'adminlte.preloader.img.alt' => $empresa->name           
                ]);
            }
        });  

        /*
        $user = auth()->user()->id_empresa;
        $empresa = Empresa::where('id',$user)
                        ->first();

        //Config::set('adminlte.preloader.img.path', 'images/logo-nawoye_op2.png');
        config(['adminlte.preloader.img.path' => 'images/logo_128.png',
                'adminlte.preloader.img.alt' => $empresa->name           
                ]);
                */
    }