Ad

Laravel 5.6 View Composer Not Working On Windows With Apache

I have a strange scenario going on. I have several View Composers in this project of mine. They've all been working fine so far but today I made a change in how I assigned a value to one of the variables that I'm sending from the composer, to the corresponding view. I did not change the name of the variable or the Composer Service Provider, or the name of the view. I just changed the value of the variable.

So the strange thing is that it works perfectly fine on my local environment (homestead 8.0.1 via virtualbox on windows host) but when I deploy to the pre-production environment (Windows Server 2016 with Apache 2.4), I get an "Undefined variable" error for any variable I try to echo in the view.

If anyone knows whether the server can play a part in how View Composers are called or executed, I'd appreciate your enlightenment. Here is my code (albeit, a bit simplified):

  1. View snippet (was not changed):
<div class="col-sm-4">
  <div class="form-group">
    <label for="county">County:</label>
    <select class="js-select2 form-control" name="county" id="county" style="width: 100%;" data-placeholder="Choose a county">
      <option></option>
      @foreach ($county ?? array() as $k => $v)
        <option value="{{ $k }}">{{ $v }}</option>
      @endforeach
    </select>
  </div>
</div>
<div class="col-sm-4">
  <div class="form-group">
    <label for="state">State:</label>
    <select class="js-select2 form-control" name="state" id="state" style="width: 100%;" data-placeholder="Choose a state">
      <option></option>
      @foreach ($state ?? array() as $k => $v)
        <option value="{{ $k }}">{{ $v }}</option>
      @endforeach
    </select>
  </div>
</div>
  1. Service Provider (was not changed):
<?php

namespace App\Providers;

use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class ComposerServiceProvider extends ServiceProvider
{

    public function boot()
    {
        View::composers([
          'App\Http\ViewComposers\MyComposer' => 'name.of.my.view',
          'App\Http\ViewComposers\OtherComposer' => ['one.view','another.view'], 
        ]);
    }

    public function register()
    {
        //
    }
}
  1. View Composer (before):
<?php

namespace App\Http\ViewComposers;

use Illuminate\View\View;
use App\ApiRepo\Dir;

class DomiciliosClientesComposer
{
  protected $domi;

  public function __construct(Dir $domi)
  {
    $this->domi = $domi;
  }

  public function compose(View $view)
  {
    $state = $this->domi->firstFunction();
    $county = $this->domi->secondFunction();

    $view->with([
      'state' => $state,
      'county' => $county,
    ]);
  }
}
  1. View Composer (after):
 <?php

    namespace App\Http\ViewComposers;

    use Illuminate\View\View;
    use App\ApiRepo\Dir;

    class DomiciliosClientesComposer
    {
      protected $domi;

      public function __construct(Dir $domi)
      {
        $this->domi = $domi;
      }

      public function compose(View $view)
      {
        $catalogues = [];
        foreach ($this->domi->newFunction() as $x) {
          $catalogues[$x["type"]][$x["id"]] = $x["value"];
        }

        $view->with([
          'domi' => array(),
          'zipcode' => array(),
          'state' => ($catalogues['state'] ?? array()),
          'county' => ($catalogues['county'] ?? array()),
        ]);
      }
    }

I tried clearing the artisan cache via a route (because I don't have ssh access to that pre-prod server), and re-uploading the entirety of the project in case there were dependencies (?) missing or something but still no luck.

Ad

Answer

I could not figure out what the problem was, specifically. But, because I needed to do this for something else, I cleared all cache (including everthing in storage folder-- other than user-submitted files) and after that, the view composer retrieved variables correctly in the pre-production environment *shrugs*.

I had previously deleted all PHP files in storage\framework\views but that did not help, so I'm guessing some other temporary file was messing things up.

Anyway, bottom line: If you encounter this problem, you could try deleting all temporary files and clearing all cache and that may do the trick, if you're lucky.

If anyone else has a different answer, please do let me know, I'm always interested in learning something new.

Ad
source: stackoverflow.com
Ad