Laravel 5无法使用AppServiceProvider中的boot与视图共享数据

时间:2015-04-26 17:46:16

标签: php laravel laravel-5

刚刚完成了L5的全新安装,根据文档,我正在尝试使用AppServiceProvider类中的简单共享方法与所有视图共享数据。

<?php namespace App\Providers;

use Illuminate\Support\ServiceProvider;
use View;

class AppServiceProvider extends ServiceProvider {

/**
 * Bootstrap any application services.
 *
 * @return void
 */

public function boot()
{
    View::share('website', 'test');
}

/**
 * Register any application services.
 *
 * This service provider is a great spot to register your various container
 * bindings with the application. As you can see, we are registering our
 * "Registrar" implementation here. You can add your own bindings too!
 *
 * @return void
 */
public function register()
{
    $this->app->bind(
        'Illuminate\Contracts\Auth\Registrar',
        'App\Services\Registrar'
    );
}

}

路线档案:

Route::get('/', function(){
return view('test');
});

刀片文件:

<h1>Test</h1>
{{ $website }}

这应该很简单,所以我想知道我是否在安装阶段犯了一个非常明显的错误。

由于

1 个答案:

答案 0 :(得分:2)

最有可能的是,您应该通过运行以下来清除compiled.php文件:

php artisan clear-compiled

或通过手动删除vendor/compiled.php(在之前的L5版本中storage/framework/compiled.php)。

以下是解释。 Laravel 预编译基本上每个请求都使用的某些类。这有助于性能优化。要编译的文件可以在config/compile.php下的files中指定。 default one看起来像这样:

'files' => [
    realpath(__DIR__.'/../app/Providers/AppServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/BusServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/ConfigServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/EventServiceProvider.php'),
    realpath(__DIR__.'/../app/Providers/RouteServiceProvider.php'),
],

这意味着如果您更改其中一个预编译文件,则不会立即应用更改(如果存在compiled.php),但只有在您再次运行php artisan optimize或运行{{1}之后清除php artisan clear-compiled文件。