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.

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

报告相同问题?

悬赏问题

  • ¥15 stm32开发clion时遇到的编译问题
  • ¥15 lna设计 源简并电感型共源放大器
  • ¥15 如何用Labview在myRIO上做LCD显示?(语言-开发语言)
  • ¥15 Vue3地图和异步函数使用
  • ¥15 C++ yoloV5改写遇到的问题
  • ¥20 win11修改中文用户名路径
  • ¥15 win2012磁盘空间不足,c盘正常,d盘无法写入
  • ¥15 用土力学知识进行土坡稳定性分析与挡土墙设计
  • ¥70 PlayWright在Java上连接CDP关联本地Chrome启动失败,貌似是Windows端口转发问题
  • ¥15 帮我写一个c++工程