Get and Set cookie in boostrap::boot

materix
Backer | 83
+
0
-

I want to set and get a cookie in the bootstrap boot-method (before models and presenters are loaded).

As I understand the methods are available in getHttpRequest() and getHttpResponse(). But how can I access these objects in the bootstrap:boot()?

dkorpar
Member | 136
+
+2
-

Real question is what are you trying to achieve…
you can allways avoid framework and write/read cookies from native php methods
https://www.php.net/…etcookie.php

you could also get instances of what you need in bootstrap.php right after creating container:

$container = $configurator->createContainer();
/** @var \Nette\Http\Request $request */
$request = $container->getByType(\Nette\Http\Request::class);
$response = $container->getByType(\Nette\Http\Response::class);

But it all depends on what you're trying to achieve and is it soon enough to do it in basepresenter on startup
or even step before on application startup
https://github.com/…lication.php#L31

Last edited by dkorpar (2022-08-04 12:19)

materix
Backer | 83
+
0
-

Thank you for the awesome reply!

you can allways avoid framework and write/read cookies from native php methods

I would like to do it the “nette-way”.

But it all depends on what you're trying to achieve

This is just some simple implementation I would like to use during development (not in production).

or even step before on application startup

Sounds interesting, how do I do that?

you could also get instances of what you need in bootstrap.php right after creating container

Thanks, I will try that 👍

dakur
Member | 493
+
+1
-

or even step before on application startup

Sounds interesting, how do I do that?

I would say it's this: https://doc.nette.org/…n/presenters#…

edit: sorry, I meant this: https://github.com/…lication.php#L34

So basically:

$container = $configurator->createContainer();
/** @var \Nette\Application\Application $application */
$application = $container->getByType(\Nette\Application\Application ::class);
$application->onStartup[] = function () {
	// ...
};

Last edited by dakur (2022-08-04 16:48)

materix
Backer | 83
+
0
-

Perfect, it works!

$application->onStartup[] = function () use($container) {
    $request = $container->getByType(\Nette\Http\Request::class);
};