[英]How to Display redirect()->to()->with() in view (CI4)
我試圖在重定向中設置一個 flashdata 但它沒有被拾取所以我轉向redirect()->to()->with()
function。但是,我不知道如何在我看來調用它。 我嘗試復制它在 Laravel 中的完成方式,但它不起作用。
這是我的重定向代碼:
return redirect()->to(base_url('categories'))->with('msg', 'You are not allowed to access that category.');
我在我看來嘗試的是:
if(session()->has('msg')){ echo session()->get('msg'); }
我一直在尋找它幾個小時,但在文檔中找不到任何內容。 感謝任何幫助。
我不確定這是否只是緩存或其他東西,但我刪除、更新了一些東西,然后放回我的舊代碼並且它起作用了。
在我的 controller 中:
$session->setFlashdata('success', false);
$session->setFlashdata('msg', 'You are not allowed to access that category.');
$session->setFlashdata('alert', 'alert-warning');
return redirect()->to(base_url('courses'));
在我看來:
<?php
if($session->getFlashdata('msg') != ''){
?>
<div class="alert <?= $session->getFlashdata('alert') ?> alert-dismissible fade show" role="alert">
<span class="text-start mb-0"><?= $session->getFlashdata('msg') ?></span>
<button type="button" class="btn-close login-btn-close" data-bs-dismiss="alert" aria-label="Close"></button>
</div>
<?php
}
?>
我還發現使用with()
與在視圖中調用getFlashdata
是一樣的:
return redirect()->to(base_url('courses'))->with('msg', 'You are not allowed to access that category.');
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.