0

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.

guradio
  • 15,524
  • 4
  • 36
  • 57
  • What if you retrieve the flashdata value into a variable and pass that to the view as per normal. Just checking the flashdata function between CI 3 and CI 4 and they are the same. As an aside, seeing as there is a session() helper to help retrieve session values directly into a view, there should be one for flashdata. I've not had a chance to play with this more. – TimBrownlaw Apr 16 '20 at 20:28

1 Answers1

0

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.

TimBrownlaw
  • 5,457
  • 3
  • 24
  • 28
  • does it still function like flashdata or do i need to unset it right after using it? – guradio Apr 17 '20 at 05:04
  • like i mention on OP i was able to do this but I goal is to use redirect so that the url changes from register to just the index of register. but thanks for idea anyway. My problem with this is that url is register not index of register – guradio Apr 17 '20 at 05:23
  • im just wondering now if it is possible to send data with `redirect()-to()` – guradio Apr 17 '20 at 05:24
  • @guradio Yes, It is using flashdata, so yes it functions like flashdata. I have provided you with working test code you can copy and paste and have a play with. – TimBrownlaw Apr 17 '20 at 08:21
  • @guradio In regards to sending data via the redirect, what have you tried? It's just a URL so you could have register/index/1234 where 1234 is "data". What kind of data are you envisaging? I would have thought the provided code would let you do what you wanted. Is something not clear? I am happy to address any points regarding it. – TimBrownlaw Apr 17 '20 at 08:24
  • i may have not been clear regarding my question..il explain with an example and please correct me if im wrong. When user is trying to register the url is /register/store. after register success/fail i want url to be the /register only and show a message. So i used redirect for that but i cant send any data like an array or string – guradio Apr 18 '20 at 13:39
  • Yes, so you set the flashdata in your register/store, and after your redirect to register/ you use it. It's the same as I have shown in the Page Controller above but reversed. Pay attention on how I pass it to the view. Although I have not actually tried it with your json_encode data as you have shown. – TimBrownlaw Apr 18 '20 at 13:48
  • my problem is if i dont change the url when i refresh the data is stored in data base again. I need to change url to avoid storing data twice or more. – guradio Apr 18 '20 at 13:59
  • @guradio Well that's a whole different issue. You can call the same controller/method and with some logic you can prevent the double save issue. You might want to create a new question for that and include your controller/methods or a sample of them. – TimBrownlaw Apr 18 '20 at 17:24