title: How to make "global" variable available in Laravel helper and view?
body: <p>I'm working on a Laravel app that I'm transitioning to React using a microfrontend(-like?) approach. I defined a helper function in Laravel that receives a component name and array of props and outputs the server-side rendered React HTML. I then call this function inside my views where needed.</p>
  
  <p>Each page in my application defines a few variables that might affect the rendering both of these React components and also the Blade templates. So I define the variables in the view and send them to JavaScript land through a global <code>window</code> variable. But I also need these variables in the SSR helper.</p>
  
  <p>Right now I have two ideas on how to do this:</p>
  
  <ol>
  <li>Pass the variables as props in each call to my helper function. I want to avoid this as the variables will never change throughout the request lifecycle and all the @includes and @extends</li>
  <li>Set the values using <code>config</code> helper before rendering the view. This seems kind of unidiomatic, as I think <code>config</code> should be used with more "static" values (that apply for the whole application and not particular pages), but I'm not very well versed in Laravel-world so this might actually be acceptable.</li>
  </ol>
  
  <p>So right now I'm more inclined to 2, but I was wondering if there was a better option?</p>
  
  <p><strong>Some code:</strong></p>
  
  <p>my-template.blade.php</p>
  
  <pre class="lang-php prettyprint-override"><code>//these are the variables that I want to access in my helper
  @extends('master.index', [
    "_PAGE_CONFIG" =&gt; [
      "page" =&gt; "blog",
      "header" =&gt; ["search" =&gt; true]
    ]
  ]);
  
  @section('content')
  
  @include('some-template.that-also-has.ssr-components-in-it')
  
  {!! ssr('blog/Blog', ["posts" =&gt; $posts]) !!}
  
  @endsection
  </code></pre>
  
  <p>master/index.blade.php</p>
  
  <pre class="lang-php prettyprint-override"><code>&lt;body&gt;
  
  @if($_PAGE_CONFIG["header"])
  &lt;header&gt;{!! ssr('header/Header') !!}&lt;/header&gt;
  @endif
  
  @yield('content')
  
  &lt;script&gt;
  //here I pass my variables to (client) JS land
  window._PAGE_CONFIG = @json($_PAGE_CONFIG);
  &lt;/script&gt;
  
  &lt;/body&gt;
  </code></pre>
  
  <p>my-SSR-helper.php</p>
  
  <pre class="lang-php prettyprint-override"><code>function ssr($component, $props = []) {
    /*
    here I call a node server or script that handles the rendering for me,
    but I want to pass it $_PAGE_CONFIG, which will be different in each page.
    I could pass it in each ssr call in the template but this is what I want to avoid
    as this function might be called several times down the @include/@extend chain
    and $_PAGE_CONFIG never changes in any one page
    (but might be different for different pages).
    */
  }
  </code></pre>