Laravel视图编辑器变量未使用服务提供程序定义



我使用服务提供商在每个视图的侧边栏上创建存档和标签。我正在关注 Laracasts 视频系列,我可以发誓我已经完成了教练所做的一切。但是,我收到一个未定义的变量错误。我一辈子都想不通。你们中有人遇到过这个问题吗?你知道我做错了什么吗?

错误消息如下(我得到两者是因为我试图注释掉存档以查看标签是否有效(:

"Undefined variable: archives (View: C:UsersAndrewHomesteadWanderlustCentreresourcesviewslayoutssidebar.blade.php) (View: C:UsersAndrewHomesteadWanderlustCentreresourcesviewslayoutssidebar.blade.php) (View: C:UsersAndrewHomesteadWanderlustCentreresourcesviewslayoutssidebar.blade.php)"
"Undefined variable: tags (View: C:UsersAndrewHomesteadWanderlustCentreresourcesviewslayoutssidebar.blade.php) (View: C:UsersAndrewHomesteadWanderlustCentreresourcesviewslayoutssidebar.blade.php) (View: C:UsersAndrewHomesteadWanderlustCentreresourcesviewslayoutssidebar.blade.php)"

我的文件如下:

应用服务提供商.php

<?php
namespace AppProviders;
use AppPost;
use AppTag;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    protected $defer = true;
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        view()->composer('layouts.sidebar', function ($view) {
            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));
        });
    }
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}
config

/app.config

'providers' => [
        /*
         * Laravel Framework Service Providers...
         */
        IlluminateAuthAuthServiceProvider::class,
        IlluminateBroadcastingBroadcastServiceProvider::class,
        IlluminateBusBusServiceProvider::class,
        IlluminateCacheCacheServiceProvider::class,
        IlluminateFoundationProvidersConsoleSupportServiceProvider::class,
        IlluminateCookieCookieServiceProvider::class,
        IlluminateDatabaseDatabaseServiceProvider::class,
        IlluminateEncryptionEncryptionServiceProvider::class,
        IlluminateFilesystemFilesystemServiceProvider::class,
        IlluminateFoundationProvidersFoundationServiceProvider::class,
        IlluminateHashingHashServiceProvider::class,
        IlluminateMailMailServiceProvider::class,
        IlluminateNotificationsNotificationServiceProvider::class,
        IlluminatePaginationPaginationServiceProvider::class,
        IlluminatePipelinePipelineServiceProvider::class,
        IlluminateQueueQueueServiceProvider::class,
        IlluminateRedisRedisServiceProvider::class,
        IlluminateAuthPasswordsPasswordResetServiceProvider::class,
        IlluminateSessionSessionServiceProvider::class,
        IlluminateTranslationTranslationServiceProvider::class,
        IlluminateValidationValidationServiceProvider::class,
        IlluminateViewViewServiceProvider::class,
        /*
         * Package Service Providers...
         */
        /*
         * Application Service Providers...
         */
        AppProvidersAppServiceProvider::class,
        AppProvidersAuthServiceProvider::class,
        //AppProvidersBroadcastServiceProvider::class,
        AppProvidersEventServiceProvider::class,
        AppProvidersRouteServiceProvider::class,
        AppProvidersSocialMediaServiceProvider::class,
    ],

资源/视图/布局/侧边栏.刀片.php

<aside class="col-md-4 blog-sidebar">
  <div class="p-3 mb-3 bg-light rounded">
    <h4 class="font-italic">About</h4>
    <p class="mb-0">Etiam porta <em>sem malesuada magna</em> mollis euismod. Cras mattis consectetur purus sit amet fermentum. Aenean lacinia bibendum nulla sed consectetur.</p>
  </div>
  <div class="p-3">
    <h4 class="font-italic">Archives</h4>
    <ol class="list-unstyled mb-0">
      @foreach ($archives as $stats)
        <li>
          <a href="/?month={{ $stats['month'] }}&year{{ $stats['year'] }}">
            {{ $stats['month'] . ' ' . $stats['year'] }}
          </a>
        </li>
      @endforeach
    </ol>
  </div>
  <div class="p-3">
    <h4 class="font-italic">Tags</h4>
    <ol class="list-unstyled mb-0">
      @foreach ($tags as $tag)
        <li>
          <a href="/posts/tags/{{ $tag }}">
            {{ $tag }}
          </a>
        </li>
      @endforeach
    </ol>
  </div>
  <div class="p-3">
    <h4 class="font-italic">Elsewhere</h4>
    <ol class="list-unstyled">
      <li><a href="#">GitHub</a></li>
      <li><a href="#">Twitter</a></li>
      <li><a href="#">Facebook</a></li>
    </ol>
  </div>
</aside>

嗨,我在 AppServiceProvider 中复制并尝试了您的代码,它工作正常,您是否尝试使用以下命令清除应用程序缓存和服务缓存:

php artisan config:clear
php artisan clear-compiled

尝试使用view()方法View立面

App\Providers\AppServiceProvider.php

<?php
namespace AppProviders;
use AppPost;
use AppTag;
use IlluminateSupportFacadesView;
use IlluminateSupportServiceProvider;
class AppServiceProvider extends ServiceProvider
{
    protected $defer = true;
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::composer('layouts.sidebar', function ($view) {
            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));
        });
    }
    /**
     * Register any application services.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

我过去遇到过同样的问题。我整理了一条路。但我不知道这是不是最好的解决方案。

public function boot()
{
        view()->composer('*', function ($view) {
            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));
        });
}

这对你有用,我相信这就是你想要的。

public function boot()
{
    View::composer('*', function($view){
        $archives= Post::archives();
        $tags = Tag::has('posts')->pluck('name');
        $view->with('categories',$archives);
        $view->with('tags',$tags);
    });
}

相关内容

最新更新