The
session
function may be used to get or set session values:<?php $value = session('key');
<?php public function flashMessage()
{
session()->flash('message', 'some-message');
}
public function redirectWithFlash()
<?php public function redirectWithFlash()
{
session()->flash('message', 'some-message');
return $this->redirect('/livewire-dusk/LegacyTests%5CBrowser%5CRedirects%5CComponent');
}
<?php public function redirectToPageTwoUsingNavigateAndDestroyingSession()
{
session()->regenerate();
return $this->redirect('/second', navigate: true);
}
<?php public function doRedirect()
{
session()->flash('alert', 'Session flash data');
$this->redirect('/redirect');
}
<?php public function mount()
{
session()->flash('alert', 'Session flash data');
}
#[On('rerender')]
<?php public function redirectIntended($default = '/', $navigate = false)
{
$url = session()->pull('url.intended', $default);
$this->redirect($url, $navigate);
}
<?php on('response', function ($response) {
if (! static::$atLeastOneMountedComponentHasRedirected && app()->has('session.store')) {
session()->forget(session()->get('_flash.new'));
}
});
<?php $component = Livewire::test(TriggersRedirectStub::class);
session()->put('url.intended', route('foo'));
$component->runAction('triggerRedirectIntended');
<?php public function flash_data_is_available_after_render()
{
session()->flash('foo', 'bar');
$this->assertEquals('bar', session()->get('foo'));
Livewire::test(RenderOnRedirectWithSkipRenderMethod::class);
<?php public function flash_data_is_available_after_render()
{
session()->flash('foo', 'bar');
$this->assertEquals('bar', session()->get('foo'));
Livewire::test(RenderOnRedirectWithSkipRenderMethod::class);
<?php Livewire::test(RenderOnRedirectWithSkipRenderMethod::class);
$this->assertEquals('bar', session()->get('foo'));
}
<?php public function flash_data_is_unavailable_after_subsequent_requests()
{
session()->flash('foo', 'bar');
$this->assertEquals('bar', session()->get('foo'));
$component = Livewire::test(RenderOnRedirectWithSkipRenderMethod::class);
<?php public function flash_data_is_unavailable_after_subsequent_requests()
{
session()->flash('foo', 'bar');
$this->assertEquals('bar', session()->get('foo'));
$component = Livewire::test(RenderOnRedirectWithSkipRenderMethod::class);
<?php $component = Livewire::test(RenderOnRedirectWithSkipRenderMethod::class);
$this->assertEquals('bar', session()->get('foo'));
$component->call('$refresh');
<?php $component->call('$refresh');
$this->assertNull(session()->get('foo'));
}