Reputation: 15555
I want to redirect route to index of same controller. My scenario is after store of data in database I want to show message. Now I tried using FlashData
like:
$this->session->setFlashdata('errmsg', json_encode(array('errstatus' => 'true' , 'msg' => $errorstring)));
So i tried accessing the data in view like :
<?php print_r($this->session->getFlashdata("errmsg"))?>
But it fails because the session variable is not existing. The way i am redirecting to same controller is like :
return redirect()->to('/register');
Now I am stuck in sending data to this redirect. I can do return view('pages/register', $this->session->get());
but its only a work around because if I do this the url still has the method store and it will show the error message everytime it loads.
Upvotes: 0
Views: 1362
Reputation: 5507
I set up a little test to have a play with this...
I created a Controller (Home) to set up a flashData value with a link to another controller (Page) that sets up a second flashData value and attempts to displays both of them.
The Home Controller (app\Controllers\Home.php)
class Home extends BaseController
{
protected $session;
public function __construct() {
$this->session = \Config\Services::session();
}
public function index()
{
$this->session->setFlashdata('errmsg_1','Error Message 1');
echo '<a href="page">Click here to View the Flash Data</a><br>';
}
}
The Page Controller (app\Controllers\Page.php)
class Page extends BaseController {
protected $session;
public function __construct() {
$this->session = \Config\Services::session();
}
public function index() {
echo __LINE__ . ' ' . __CLASS__ . ' ' . __METHOD__;
$this->session->setFlashdata('errmsg_2','Error Message 2');
return redirect()->to('/page/register');
}
public function register() {
echo __LINE__ . ' ' . __CLASS__ . ' ' . __METHOD__;
$page['error_message_1'] = $this->session->getFlashdata('errmsg_1');
$page['error_message_2'] = $this->session->getFlashdata('errmsg_2');
echo view('page_view', $page);
}
}
And the View (app\Views\page_view.php)
<?php
/**
* @var string $error_message_1
* @var string $error_message_2
*/
echo '<br>';
echo 'Error 1 :';
echo ($error_message_1) ? $error_message_1 : 'I am Null'; // Expect this to be NULL?
echo '<br>';
echo 'Error 2 :';
echo ($error_message_2) ? $error_message_2 : 'I am Null'; // This should appear
echo '<br><a href="/home">Go Back Home</a><br>';
So the Home Controller loads up the FlashData for "errmsg_1" with a link to take us to the Page Controller.
Once at the Page Controller, the index sets an additional errmsg_2 and performs a redirect to the register method.
So after clicking the link from the Home Controller we arrive at the Page Controller which displays...
17 App\Controllers\Page App\Controllers\Page::register
Error 1 :I am Null
Error 2 :Error Message 2
Go Back Home
And on performing a Refresh on the Page Controller we get...
17 App\Controllers\Page App\Controllers\Page::register
Error 1 :I am Null
Error 2 :I am Null
Go Back Home
So the redirect does work using flashData.
The only downside is that you have to pass the flashdata value as a variable into the view to display it.
Upvotes: 0