CodeIgniter 4 - 如何在视图中显示 Flashdata?
CodeIgniter 4 - How to display Flashdata inside a View?
我正在将我的项目从 CodeIgniter 3 升级到 CodeIgniter 4,
我正在尝试在视图中显示 flashdata 消息,但不幸的是,我尝试的每种方法都出现了不同的错误。
在 CodeIgniter 3 中,我曾经这样调用:
<?php if ($this->session->flashdata('message')) : ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $this->session->flashdata('message'); ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
我在 CodeIgniter 4 中尝试了同样的操作,但出现了这个错误:
ErrorException
Undefined property: CodeIgniter\View\View::$session
任何人都可以告诉我如何实现这一目标吗?
提前致谢。
context ($this) 是 View 的一个实例 class --> 你不能直接访问 Session 实例
您可以在下方新建实例
$session = \Config\Services::session();
我只是用另一种方式来显示闪存数据,它工作正常。
在我的控制器中,我为传递给视图的数据添加了一个新索引:
$data['message'] = "Sorry, you must login first";
return view('login', $data);
然后在视图中login.php
我这样称呼它:
<?php if (isset($message)) : ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
更新:
我只是使用 markAsFlashdata()
方法,而且效果很好。这是我在 return
方法之前在控制器中所做的:
$_SESSION['error'] = 'Sorry, you must login first';
$session = session();
$session->markAsFlashdata('error');
然后在视图中我使用 $_SESSION['error']
:
访问闪存数据
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-warning" role="alert">
<?= $_SESSION['error']; ?>
</div>
<?php endif;?>
在 CodeIgniter 4 中设置 Flash 数据 $session->setFlashdata('item', 'value');
和查看 $session->getFlashdata('item');
的新方法
在 echo $this->section('content');
之后添加这一行
$session = \Config\Services::session();
<?php if (isset($message)) : ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
可以直接使用session()函数:
<?php if (session()->getFlashdata('message') !== NULL) : ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo session()->getFlashdata('message'); ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
我正在将我的项目从 CodeIgniter 3 升级到 CodeIgniter 4, 我正在尝试在视图中显示 flashdata 消息,但不幸的是,我尝试的每种方法都出现了不同的错误。
在 CodeIgniter 3 中,我曾经这样调用:
<?php if ($this->session->flashdata('message')) : ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo $this->session->flashdata('message'); ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
我在 CodeIgniter 4 中尝试了同样的操作,但出现了这个错误:
ErrorException
Undefined property: CodeIgniter\View\View::$session
任何人都可以告诉我如何实现这一目标吗? 提前致谢。
context ($this) 是 View 的一个实例 class --> 你不能直接访问 Session 实例
您可以在下方新建实例
$session = \Config\Services::session();
我只是用另一种方式来显示闪存数据,它工作正常。
在我的控制器中,我为传递给视图的数据添加了一个新索引:
$data['message'] = "Sorry, you must login first";
return view('login', $data);
然后在视图中login.php
我这样称呼它:
<?php if (isset($message)) : ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
更新:
我只是使用 markAsFlashdata()
方法,而且效果很好。这是我在 return
方法之前在控制器中所做的:
$_SESSION['error'] = 'Sorry, you must login first';
$session = session();
$session->markAsFlashdata('error');
然后在视图中我使用 $_SESSION['error']
:
<?php if (isset($_SESSION['error'])): ?>
<div class="alert alert-warning" role="alert">
<?= $_SESSION['error']; ?>
</div>
<?php endif;?>
在 CodeIgniter 4 中设置 Flash 数据 $session->setFlashdata('item', 'value');
和查看 $session->getFlashdata('item');
在 echo $this->section('content');
$session = \Config\Services::session();
<?php if (isset($message)) : ?>
<div class="alert alert-warning alert-dismissible fade show" role="alert">
<?php echo $message; ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>
可以直接使用session()函数:
<?php if (session()->getFlashdata('message') !== NULL) : ?>
<div class="alert alert-success alert-dismissible fade show" role="alert">
<?php echo session()->getFlashdata('message'); ?>
<button type="button" class="close" data-dismiss="alert" aria-label="Close"><span aria-hidden="true">×</span></button>
</div>
<?php endif; ?>