1

Edit: Ali Ismaeel provided a great answer to my structure, but I had done a poor job of asking the question and re-wrote it. My edit history shows what he was addressing. Sorry, I'm trying to get better at asking good questions.

I have some custom middleware set up that sets the localization based on some priorities. The middleware works as expected, but because it is Middleware, it continues to run. Now normally if something executes multiple times and I only need it once, I would set a flag as a variable or session and just check its status. I can't seem to replicate this behavior with Middleware, and I believe it's because I do not know where to put it.

class Localization {

    public function handle(Request $request, Closure $next): Response {

        # Set Already Run Flag
        if ($request->has('hasRun')) {
            # Return Before Code Runs
            return $next($request);
        }

        # Run Code
        # Then Set Variable
        $request->attributes->set('hasRun', true);

        return $next($request);
    }
}

Thank you!

1 Answer 1

1

It is normal that the middleware runs every time a request made on the related route. so if the middleware is applied on a group of routes , it will be applied every single time a request made on any of these routes, it is the normal behavior.

So , the middleware will run always when a request made on any related route, and the middleware responsibility is to make changes on the request if it is required or let the request proceed to its next step.

A better design of the middleware is to contain only one return $next($request);at the end, and any required changes can be done before it.

I rewrite your code to illustrate what I mean.

class Localization {

    private array $validLocales;

    public function __construct() {
        # Get Active Locales - From Locale Model
        $this->validLocales = Locale::getActiveLocales();
    }

    public function handle(Request $request, Closure $next): Response {

        if (!$request->has('localeSet')) {
            # Check Session
            $locale = $request->session()->get('locale');
            if ($locale) {
                App::setLocale($locale);
                $request->attributes->set('localeSet', true);
                DebugLog('info', '[MW - Localization] Locale set from session.', ['locale' => $locale], 'init');
            }else{
                # Default to English
                App::setLocale('en');
                Session::put('locale', 'en');
                $request->attributes->set('localeSet', true);
                DebugLog('info', '[MW - Localization] Locale set to default.', ['locale' => 'en'], 'init');
            }
        }
        return $next($request);
    }
}


Sign up to request clarification or add additional context in comments.

1 Comment

You gave a wonderful response and that helped me clean up the code. I edited my original post, as I did a poor job of narrowing the scope of my question. Because the middleware code will not change after the initial page load, it should be able to return with the original values that would be cached. I can reset a variable or session in a blade template and it does prevent the repetition, but I know that isn't the correct spot to do it. It doesn't seem to work when I reset the variable or session in AppServiceProvider.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.