Be part of JetBrains PHPverse 2026 on June 9 – a free online event bringing PHP devs worldwide together.

stratboy's avatar

How to share data to all views/components/blate templates?

Hi, I'd like to make available both a couple of functions an an array of data to all the app views. Where's the right place and way of doing it? From the docs, it seems for example that I could do it with View::share from AppServiceProvider->boot. But what if I want to also share functions?

And also: to build the data that I need to pass to my views, I have to read and parse public/build/manifest.json. But I'd like this operation to be executed only 1 time per session, not 1 time per request (I don 't want the app to load and parse that file every time the user clicks some link). So where to put it?

0 likes
1 reply
LaryAI's avatar
Level 58

To share data across all views in a Laravel application, you can indeed use the View::share method within the boot method of the AppServiceProvider. This is the right place to share data that is needed globally across views.

For sharing functions, you can use view composers or create helper functions. Helper functions can be autoloaded by Composer and made available globally.

Regarding the manifest.json file, you can parse it once and store the result in the session or cache, depending on your needs. This way, you avoid parsing the file on every request.

Here's how you can implement this:

  1. Create a helper function to parse manifest.json and store it in the session or cache.
function getManifestData() {
    // Check if data is already stored in the session or cache
    $manifestData = session('manifest_data') ?? cache('manifest_data');
    
    if (!$manifestData) {
        // Parse the manifest.json file
        $manifestPath = public_path('build/manifest.json');
        $manifestData = json_decode(file_get_contents($manifestPath), true);
        
        // Store in the session or cache
        session(['manifest_data' => $manifestData]);
        // Or use cache, with a specified duration
        // cache(['manifest_data' => $manifestData], now()->addMinutes(60));
    }
    
    return $manifestData;
}
  1. Register the helper function by creating a new file for your helpers, for example, app/Helpers/GlobalHelpers.php, and load it using Composer's autoload feature by updating composer.json:
"autoload": {
    "files": [
        "app/Helpers/GlobalHelpers.php"
    ]
}

After updating composer.json, run composer dump-autoload to regenerate the autoload files.

  1. Share the data and functions using View::share in the boot method of AppServiceProvider:
use Illuminate\Support\Facades\View;
use Illuminate\Support\ServiceProvider;

class AppServiceProvider extends ServiceProvider
{
    public function boot()
    {
        // Share data across all views
        View::share('sharedData', 'This is shared data');

        // Share the manifest data
        View::share('manifestData', getManifestData());

        // Share a function
        View::composer('*', function ($view) {
            $view->with('sharedFunction', function () {
                // Your shared function logic here
                return 'This is a shared function result';
            });
        });
    }
}

With this setup, you can access $sharedData, $manifestData, and call $sharedFunction() in any view. Remember that if you use the session to store the manifest data, it will persist only for the current user session. If you want the data to be globally cached across sessions, use Laravel's cache system instead.

Please or to participate in this conversation.