Laravel 5.4:将变量解析为视图的最佳实践

For example, I want to add a variable in my default view (default.blade.php)

I can of course, define my variable directly into the view file, like this :

@php ($user = Sentinel::getUser())

But this is not recommended.

Should I add it on AppServiceProvider.php ? (https://laravel.com/docs/5.4/views#sharing-data-with-all-views)

But with which call ? Like this :

public function boot()
    { $user = Sentinel::getUser(); }

This get : Undefined variable: user

public function boot()
    { View::share('user', Sentinel::getUser()); }

This get Trying to get property of non-object, so Sentinel is not truly declare

Or in the a controller

public function __construct()
    {
        //user
        $user = Sentinel::getUser();
        view()->share('user',$user);            

    }

I also try this into my controller

public function boot()
    {
        return view('layouts/default')->with('user', Sentinel::getUser(););
    }

or

public function boot()    {
        view()->composer('layouts.default', function($view)     {
            $view->with('user', Sentinel::getUser());

        });     
    }

Still get "Undefined variable: user"

In your controller, define a function like that:

public function index() {
   $user = Sentinel::getUser();

   //The parameter of the view is your blade file relative to your directory
   //resources/views/default.blade.php
   return view('default')->with('user', $user);
}

In your web.php (routes file), add this

//First parameter is the url, second is the controller/function
Route::get('/', 'YourControllerName@index');

Now test the view in localhost [Optional]

http://localhost:8000

The idea is to call View::XXXXX within the boot method of a service provider...

The simplest is to call share on the view within your app service provider... this will make it available to all views... however take notice of when this value is resolved, it is resolved at boot time...

class AppServiceProvider extends ServiceProvider
{
    /**
     * Bootstrap any application services.
     *
     * @return void
     */
    public function boot()
    {
        View::share('key', 'value');
    }

    /**
     * Register the service provider.
     *
     * @return void
     */
    public function register()
    {
        //
    }
}

You can also create a view composer that will be run prior to rendering a specific view or set of views... You can give it the view/class or view/closure... this will be evaluated before the view renders...

Bind a view composer class:

public function boot()
    {
        // Using class based composers...
        View::composer(
            'profile', 'App\Http\ViewComposers\ProfileComposer'
        );
}

Or attach a closure... and do what you need to boot that vie within that closure...

public function boot()
    {
        // Using Closure based composers...
        View::composer('dashboard', function ($view) {
            //
        });
}

You can bind to all views like the View::share... but push the evaluation to before the view render by using a simple closure based composer... in your case...

        // Using Closure based composers...
        View::composer('dashboard', function ($view) {
            $view->with('user', Sentinel::getUser() );
        });

Hope this helps...