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!