CodeIgniter 4 -如何在视图中显示Flash数据?

wljmcqd8  于 2023-02-20  发布在  其他
关注(0)|答案(6)|浏览(173)

我正在将我的项目从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">&times;</span></button>
    </div>
<?php endif; ?>

我在CodeIgniter 4中尝试了相同的操作,但得到了以下错误:

ErrorException
Undefined property: CodeIgniter\View\View::$session

有谁能告诉我如何做到这一点吗?提前感谢。

eyh26e7m

eyh26e7m1#

可以直接使用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">&times;</span></button>
    </div>
<?php endif; ?>
c7rzv4ha

c7rzv4ha2#

上下文($this)是View类的一个示例--〉您不能直接访问Session示例
您可以在下面创建新示例

$session = \Config\Services::session();
yws3nbqq

yws3nbqq3#

在CodeIgniter 4中,设置闪存数据$session->setFlashdata('item', 'value');和查看$session->getFlashdata('item');的新方法
您可以在这里查看:在CodeIgniter的会话中设置闪存数据

xienkqul

xienkqul4#

我只是用另一种方式来显示一个flashdata,它工作得很好。
在我的控制器中,我为传递给视图的数据添加了一个新索引:

$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">&times;</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;?>
5us2dqdw

5us2dqdw5#

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">&times;</span></button>
    </div>
<?php endif; ?>
1cklez4t

1cklez4t6#

目前,我的解决方案是在BaseController.php中创建一个视图方法,其思想是向$data数组中添加更多的“公共”信息。

/* In BaseController.php */

    /**
     * view function replaced 
     */
    public function view(string $name, array $data = [], array $options = []): string
    {
        // Inject global data        
        $data = array_merge($data,["controller" => $this]);        
        return view($name,$data,$options);
    }

    /**
     * Temporary message
     */
    public function flash($message, $type = 'info') {
        $this->session->setFlashdata('flash_message', 
            ["message" => $message, "type" => $type]);        
    }

    public function getFlash() {
        return $this->session->getFlashdata('flash_message');
    }

   /* In the descendant controller */
   return $this->view('products/list',['products' => $products]);

   /* In the view */
   <div id="messages">
      <?php if($flash = $controller->getFlash()) : ?>
          <?= view_cell('Base::alert', $flash); ?>
      <?php endif ?>
   </div>

相关问题