Laravel视图编写器变量未使用服务提供者定义

时间:2018-03-03 20:59:12

标签: php laravel

我使用服务提供商在侧栏上为每个视图创建档案和标签。我正在关注Laracasts视频系列,我可以发誓,我已经完成了教练所做的一切。但是我得到一个未定义的变量错误。我无法弄清楚为什么我的生活。有没有人有这个问题?你知道我做错了吗?

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

"Undefined variable: archives (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php)"

"Undefined variable: tags (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php) (View: C:\Users\Andrew\Homestead\WanderlustCentre\resources\views\layouts\sidebar.blade.php)"

我的文件如下:

AppServiceProvider.php

<?php

namespace App\Providers;

use App\Post;
use App\Tag;

use Illuminate\Support\ServiceProvider;

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()
    {
        //
    }
}

配置/ app.config中

'providers' => [

        /*
         * Laravel Framework Service Providers...
         */
        Illuminate\Auth\AuthServiceProvider::class,
        Illuminate\Broadcasting\BroadcastServiceProvider::class,
        Illuminate\Bus\BusServiceProvider::class,
        Illuminate\Cache\CacheServiceProvider::class,
        Illuminate\Foundation\Providers\ConsoleSupportServiceProvider::class,
        Illuminate\Cookie\CookieServiceProvider::class,
        Illuminate\Database\DatabaseServiceProvider::class,
        Illuminate\Encryption\EncryptionServiceProvider::class,
        Illuminate\Filesystem\FilesystemServiceProvider::class,
        Illuminate\Foundation\Providers\FoundationServiceProvider::class,
        Illuminate\Hashing\HashServiceProvider::class,
        Illuminate\Mail\MailServiceProvider::class,
        Illuminate\Notifications\NotificationServiceProvider::class,
        Illuminate\Pagination\PaginationServiceProvider::class,
        Illuminate\Pipeline\PipelineServiceProvider::class,
        Illuminate\Queue\QueueServiceProvider::class,
        Illuminate\Redis\RedisServiceProvider::class,
        Illuminate\Auth\Passwords\PasswordResetServiceProvider::class,
        Illuminate\Session\SessionServiceProvider::class,
        Illuminate\Translation\TranslationServiceProvider::class,
        Illuminate\Validation\ValidationServiceProvider::class,
        Illuminate\View\ViewServiceProvider::class,

        /*
         * Package Service Providers...
         */

        /*
         * Application Service Providers...
         */
        App\Providers\AppServiceProvider::class,
        App\Providers\AuthServiceProvider::class,
        //App\Providers\BroadcastServiceProvider::class,
        App\Providers\EventServiceProvider::class,
        App\Providers\RouteServiceProvider::class,
        App\Providers\SocialMediaServiceProvider::class,

    ],

资源/视图/布局/ sidebar.blade.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>

4 个答案:

答案 0 :(得分:1)

您好我在AppServiceProvider中复制并尝试了您的代码,它运行正常,您是否尝试清除应用程序缓存和服务缓存:

import java.util.*;
import java.lang.*;
import java.io.*;

class Mutex {

  private int val;
  public Mutex(int val) {

    this.val=val;
  }

  public int getVal() {
    return val;
  }

  public void printVal() {
    int i=0;
    while (true) {

     System.out.println(Thread.currentThread().getName()+" "+this.getVal());
      if(i==50)
        break;
      else
        i++;
    }
  }

  public static void main(String args[]) {
    final Mutex m1=new Mutex(1);
    final Mutex m2 = new Mutex(2);
    Thread t1 = new Thread(new Runnable() {
      @Override
      public void run() {
        m1.printVal();
      }
    },"THREAD-1");
    t1.start();
    t1.join(); //it will lead thread 1 to finish first
    new Thread(new Runnable() {
      @Override
      public void run() {
        m2.printVal();
      }
    },"THREAD-2").start();
  }
}

答案 1 :(得分:0)

尝试使用process launch --stop-at-entry -- ~/path-to-filename 方法

View外观实例

应用\提供商\ AppServiceProvider.php

view()

答案 2 :(得分:0)

我过去遇到过同样的问题。我找到了一个方法。但我不知道这是最好的解决方案。

public function boot()
{
        view()->composer('*', function ($view) {

            $view->with('archives', Post::archives());
            $view->with('tags', Tag::has('posts')->pluck('name'));

        });
}

答案 3 :(得分:0)

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

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