duan1979768678 2015-04-26 17:46
浏览 377
已采纳

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

Just done a fresh install of L5 and, as per the documentation, i'm trying to share data with all views using a simple share method in the AppServiceProvider class.

<?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 file:

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

blade file:

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

This should be really easy, so i'm wondering if i've made a very obvious mistake at the installation stage.

Thanks

  • 写回答

1条回答 默认 最新

  • donglaohua1671 2015-04-26 17:52
    关注

    Most probably you should clear the compiled.php file by running:

    php artisan clear-compiled
    

    or by manual deleting vendor/compiled.php (in previous L5 versions it's storage/framework/compiled.php).


    Here's the explanation. Laravel pre-compiles certain classes that are used on basically every request. This serves the purpose of performance optimization. Files to compile can be specified in config/compile.php under files. The default one looks like this:

    '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'),
    ],
    

    That means if you change one of those precompiled files, changes won't be applied immediately (if compiled.php exists) but only after you run php artisan optimize again or after you run php artisan clear-compiled to clear the compiled.php file.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥20 有人知道这种图怎么画吗?
  • ¥15 pyqt6如何引用qrc文件加载里面的的资源
  • ¥15 安卓JNI项目使用lua上的问题
  • ¥20 RL+GNN解决人员排班问题时梯度消失
  • ¥15 统计大规模图中的完全子图问题
  • ¥15 使用LM2596制作降压电路,一个能运行,一个不能
  • ¥60 要数控稳压电源测试数据
  • ¥15 能帮我写下这个编程吗
  • ¥15 ikuai客户端l2tp协议链接报终止15信号和无法将p.p.p6转换为我的l2tp线路
  • ¥15 phython读取excel表格报错 ^7个 SyntaxError: invalid syntax 语句报错