0
votes

I see from the CakePHP 3.2 documentation that to configure a session I need to use write(), so I tried that in my controller like this:

use App\Controller\AppController;
use Cake\Core\Configure;

class RatingsController extends AppController
{
    public function initialize()
    {
        parent::initialize();
        $this->loadComponent('RequestHandler');
        $this->loadComponent('Paginator');
        Configure::write('Session', ['defaults' => 'php']);
   }
}

But this doesn't seem to set up a $_SESSION array if executed in my controller.

I thought I had a workaround by setting up Auth, and thereby was able to access $_SESSION, but then when I open the controller by adding $this->Auth->allow(); to the init above, the session variable no longer exists.

Where do I need to configure Cake to start a session?

1
Why do you want to access $_SESSION in the first place? You shouldn't access any superglobals directly when using CakePHP! - ndm
I need a persistent variable. I can use $this->request->session()->read() just fine, if $_SESSION was created by cake. - Matt
My question was more like why you would want to access $_SESSION directly (not why you want to use sessions at all), as this is what your questions sounds like as it stands. - ndm
Thanks for the discussion. Answer section solves my problem. - Matt

1 Answers

3
votes

CakePHP binds sessions to the Request, e.g. set a value to a key in your Controller:

$this->request->session()->write('defaults', 'php')

Then, e.g. in your Template read the Session by key:

$this->request->session()->read('defaults')