简体   繁体   English

Laravel 5.6 View Composer 无法在带有 Apache 的 Windows 上运行

[英]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.我的这个项目中有几个 View Composer。 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.我没有更改变量或 Composer 服务提供者的名称,或视图的名称。 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.所以奇怪的是,它在我的本地环境(通过 Windows 主机上的 virtualbox 的 homestead 8.0.1)上运行得非常好,但是当我部署到预生产环境(带有 Apache 2.4 的 Windows Server 2016)时,我得到一个“未定义的变量” " 我尝试在视图中回显的任何变量的错误。

If anyone knows whether the server can play a part in how View Composers are called or executed, I'd appreciate your enlightenment.如果有人知道服务器是否可以在 View Composer 的调用或执行方式中发挥作用,我将不胜感激。 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.我尝试通过路由清除 artisan 缓存(因为我没有访问该预生产服务器的 ssh 访问权限),并重新上传整个项目,以防存在依赖项(?)丢失或其他问题,但仍然没有运气.

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* .但是,因为我需要为其他事情做这件事,所以我清除了所有缓存(包括存储文件夹中的所有内容——用户提交的文件除外),之后,视图编写器在预生产环境中正确检索了变量*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.我之前删除了 storage\\framework\\views 中的所有 PHP 文件,但这没有帮助,所以我猜其他一些临时文件把事情搞砸了。

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.如果其他人有不同的答案,请告诉我,我一直对学习新东西很感兴趣。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM