2

Im using a view composer to store global variable on all views $currency from sessions() for all views.

My View Composer.php

public function __construct(
    protected Request $request,
) {}

public function compose(View $view): void
{
    $currency = $this->request->session()->get('currency', config('app.currency') );

app\Providers\ViewServiceProvider.php

public function boot()
{
    Facades\View::composer('*', CurrencyComposer::class);
}

But my problem is im getting following error on the error pages 404 page

Session store not set on request

I have tired the soloution found - here

protected $middleware = [
    //...
    \Illuminate\Session\Middleware\StartSession::class,
    \Illuminate\View\Middleware\ShareErrorsFromSession::class,
 ];

Which does fix my issue but then im unable to set any sessions it will refresh all session on every page load.

Tony Sawlwin
  • 146
  • 1
  • 19
  • Can you show your route file ? To use sessions, your route must use the web middleware. – Thibaut Feb 25 '23 at 10:38
  • issue is on error pages eg 404. Doesnt have a route file for. – Tony Sawlwin May 06 '23 at 05:06
  • It seems like the error is caused because the session is not available in the error pages (like 404). One possible solution to this issue is to add a middleware that ensures the session is available on these pages as well. – Xaib Aslam May 06 '23 at 12:46
  • Yeah I have tired a if statement - if($this->request->session()) but same error. How do I add a middleware to error pages tho? – Tony Sawlwin May 07 '23 at 05:12
  • store all global variable in seesion so we don't need to do run mysql query every time. I thinking the same to cache somewhere and don't realize the session. Thanks for inspiration i will check and may be help you – Puneet Sharma May 09 '23 at 13:33
  • Can I ask if your views are inside the `views` without subfolders ? or it is inside the `subfolder` inside the `views` folder ? Like `views/pages/page.blade.php` something or `views/page.blade.php` ? – xenooooo May 10 '23 at 09:13
  • @xenooooo - So im mostly using livewire and all views are in either `views/livewire/guest` or `views/livewire/admin` – Tony Sawlwin May 11 '23 at 07:52

3 Answers3

0

As you may have already found out, the session is only available when the StartSession middleware is applied to the route.

Alternatively, you can also consider using the cookie, which is available to all routes. For example, you can create a custom view for the 404 error page at resources/views/errors/404.blade.php, and read the cookie using the request() helper."

<?php
$cookie = request()->cookie('xxx-you-used-for-the-currency');

echo $cookie;

ken
  • 13,869
  • 6
  • 42
  • 36
  • I dont actually need the currency for the 404 error pages. I only need the currency (session) for all other pages. But because im using the * to load the veiw composser its also loading for the 404 page. – Tony Sawlwin May 08 '23 at 05:01
  • @TonySawlwin you can use cookie for other pages as well, if you do not need it for the 404 page – ken May 08 '23 at 06:44
0

You can just do the wildcard with the folder name :

Facades\View::composer('livewire/*', CurrencyComposer::class);

It will only share the views under the livewire folder inside the views folder. Or if you want more specific

Facades\View::composer(['livewire/admin/*','livewire/guest/*'], CurrencyComposer::class);
xenooooo
  • 1,106
  • 1
  • 2
  • 8
  • definetely a good solution but can still cause a issue if wanting the comproser to be available outside of livewire. ended up going to closure based composers as seemed most suited. – Tony Sawlwin May 13 '23 at 05:18
0

Use a closure based composers in ViewServiceProvider. Using a if statement to check if session is set before setting the $view.

app\Providers\ViewServiceProvider.php

public function boot()
{
     Facades\View::composer('*', function ($view) {
          if (session()->has('currency')) {
               ...

               $view->with([
                'currency' => $currency,
               ]);
          }
     }
}
Tony Sawlwin
  • 146
  • 1
  • 19