将会话id解析为laravel刀片组件



我正在尝试从livewire迁移到laravel刀片组件。我在将变量解析为嵌套组件时遇到了一个问题。

在livewire上一切都很好。

我的观点是这样组织的:

路由转到返回布局视图的控制器。布局有两个组件,即x页眉和x页脚。在x-footer组件中,我有另一个组件x-botman。

x-botman需要一个可变的$session。

在app/View/Components/botman.php中,我尝试了几种将会话发送到视图的方法:

<?php
namespace AppViewComponents;
use IlluminateSupportFacadesSession;
use IlluminateViewComponent;
class botman extends Component
{
/**
* Session id.
*
* @var string
*/
public $session; // to be able to get it from <x-botman :session="$session" />
/**
* Create a new component instance.
*
* @return void
*/
public function __construct($session)
{
// to be able in both ways if $session is sended by tag or load it directly from 
// Session::getId()
if (!isset($session)) {
$this->session = ['session' => Session::getId()];
} else {
$this->session = $session;
}
}
/**
* Get the view / contents that represent the component.
*
* @return IlluminateContractsViewView|Closure|string
*/
public function render()
{
return view('components.botman', ['session' => Session::getId()]);
}
}

我还尝试了内部组件footer.blade.php,并在footer.php 中有来自botman.php的代码

<x-botman :session="$session" />

我一直收到错误500,因为botman.blade.php 中不存在变量$session

我的工作livewire只在返回视图中发送变量,它可以工作,但在组件上不可以:

<?php
namespace AppHttpLivewire;
use IlluminateSupportFacadesSession;
use LivewireComponent;
class Botman extends Component
{
public function render()
{
return view('livewire.botman', ['session' => Session::getId()]);
}
}

将变量解析为嵌套组件的最简单方法是什么?

PHP 8.1.11||Laravel 9.30.0

@if(Session::has('error'))
<div class="alert alert-danger">
{{ Session::get('error')}}
</div>
@endif

最新更新