Gabotronix
1 month ago
134
4
General

Can't use laravel singleton inside Middleware

Posted 1 month ago by Gabotronix

I have a custom middleware where I want to use a singleton I use to pass php variables into my frontend, however I get the following error:

 ReflectionException (-1)
Class App\Http\Middleware\Javascript does not exist

My middleware:

<?php
namespace App\Http\Middleware;


use Closure;
use Illuminate\Http\Request;
use Auth;



class AuthAdmin
{


    public function handle($request, Closure $next)
    {
        $user = Auth::user();

        if($user && $user['privileges'] > 2)
        {
            return $next($request);   
        }

        app(Javascript::class)->put(['showLoginModal' => true]);

        return redirect('/');
        
    }
}

My ServiceProvider:

<?php
namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use App\Helpers\Javascript;


class JavascriptServiceProvider extends ServiceProvider
{
    public function register()
    {
        $this->app->singleton(Javascript::class, Javascript::class);
    }
}

composer dump-autoload didn't fix anything, I have been having problems where the Javascript Class is not found for some reason, any ideas or sugestions?

Please sign in or create an account to participate in this conversation.