Welcome Guest, Not a member yet? Register   Sign In
How use controller property after requests
#1

Hello,
How we can endure a state of a controller property after a request in CodeIgniter version 4. For example, imagine that $a is a complex object:


/ the controller complains that the variable $a must not be accessed before initialization
Code:
class Example extends BaseController
{
    private int $a;

   public function index()
   {
       $this->a = 7;
      return view('welcome');
   }

   public function checkVariable()
   {
      // the code igniter complains that $a must not be accessed before initialization, but it was when index() was called
      var_dump($a);
   }
}

Some other languages we have something to sustain the state of those variable. What we have on CodeIgniter? Are there some helper to import to do that for us?

Thanks in advance.
Reply
#2

(This post was last modified: 05-05-2020, 07:19 AM by danielsonsilva.)

I found the solution using sessions.
I can add $this->session = \Config\Services::session(); into BaseController
Then, in the end of every method on my controller, I can save the state of $a into the session and in the begin I can get that state and assign into the controller property.
Reply




Theme © iAndrew 2016 - Forum software by © MyBB